如何移除matplotlib 3D绘图中的全部刻度与刻度标签
解决方法
你现有代码没有彻底清除刻度的根本原因是只隐藏了刻度的显示元素,没有直接清空刻度配置,补充3行清空刻度的代码即可实现需求。
需要在你原有坐标轴设置代码部分新增以下内容:
# 直接清空三个轴的刻度配置,从根源上清除所有刻度相关元素 ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([])
修改后完整可运行代码
from mpl_toolkits.mplot3d import Axes3D import numpy as np import matplotlib.pyplot as plt from scipy import interpolate fig = plt.figure() ax = fig.gca(projection='3d') # Plot curve and lines x = [0,0.1,1,2,3,3.8,4,3.8,3,2,1,0.11,0] y = [0,0.5,1,0.2,1,0.5,0,-0.5,-1,-0.7,-1,-0.5,0] tck, u = interpolate.splprep([x, y], s=0) unew = np.arange(0, 1.01, 0.01) out = interpolate.splev(unew, tck) ax.plot([x[1],x[1]+1],[y[1],y[1]],[0,4], color = 'red', label='vector e') for (xi,yi) in zip(x[2:len(x)-1],y[2:len(y)-1]): if (xi,yi) == (4,0): continue ax.plot([xi,xi+1],[yi,yi],[0,4], color = 'red') ax.plot(out[0], out[1], zs=0, zdir='z', color = 'blue', label='curve c') ax.plot(out[0]+1, out[1], zs=4, zdir='z', color = 'blue') # Make legend, set axes limits and labels ax.legend() ax.set_xlim(-1, 6) ax.set_ylim(-2, 2) ax.set_zlim(0, 5) ax.grid(False) # 新增:清空所有刻度配置 ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([]) # 隐藏轴线 color_tuple = (1.0, 1.0, 1.0, 0.0) ax.w_xaxis.line.set_color(color_tuple) ax.w_yaxis.line.set_color(color_tuple) ax.w_zaxis.line.set_color(color_tuple) plt.show()
修改后运行即可完全移除所有坐标轴的刻度、刻度标签与轴线,仅保留绘图平面和图例。
内容的提问来源于stack exchange,提问作者question
相关产品推荐
相关产品推荐

