当我绘制Axes3D对象时,X / Y轴上的刻度数会以某种方式自行改变(例如,对于1x1对象,每个轴上有五个刻度,而对于2x2对象,每个轴上有7个刻度。轴,见下面的截图)
1x1对象的3D绘图:
2x2对象的3D绘图:
问题是我的刻度标签数量低于刻度数,因此所有刻度标签都移动到轴的开头。
那么,我如何减少/设置滴答数?
这是我的代码:
my_w = 2
my_h = 2
x1_list_int = []
x2_list_int = []
y1_list_int = [[],[]]
y1_list_int = [[0 for x in range(my_w)] for y in range(my_h)] #matrix initialization
for i in xrange(my_w):
print i
x1_list_int.append(i*10)
x2_list_int.append(i+1)
for i in xrange(my_w):
for j in xrange(my_h):
y1_list_int[i][j] = (i-3)*(j-2)+20
data = np.array(y1_list_int)
column_names = x2_list_int
row_names = x1_list_int
fig = plt.figure()
ax = Axes3D(fig)
lx= len(data[0]) # Work out matrix dimensions
ly= len(data[:,0])
xpos = np.arange(0,lx,1) # Set up a mesh of positions
ypos = np.arange(0,ly,1)
xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25)
xpos = xpos.flatten() # Convert positions to 1D array
ypos = ypos.flatten()
zpos = np.zeros(lx*ly)
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = data.flatten()
ax.bar3d(xpos,ypos,zpos, dx, dy, dz, color='#00ceaa')
ax.w_xaxis.set_ticklabels(column_names)
ax.w_yaxis.set_ticklabels(row_names, rotation = 0)
label_x1 = 'X1'
label_x2 = 'X2'
label_y1 = 'Y1'
ax.set_xlabel(label_x2)
ax.set_ylabel(label_x1)
ax.set_zlabel(label_y1)
#-- save plot to the file
plt.savefig(self.picture_file_path_1)
....
plt.close() # final. data clean-up
答案 0 :(得分:0)
我找到了解决方案。这是:
from matplotlib.ticker import MaxNLocator
.....
ax.w_yaxis.set_major_locator(MaxNLocator(len(x1_list_int)+1))
ax.w_xaxis.set_major_locator(MaxNLocator(len(x2_list_int)+1))
.....