How to reduce number of ticks in Axes3D object (in Matplotlib) -
when plot axes3d object, number of ticks on x/y axes somehow changing (for example, 1x1 object, there 5 ticks on each axes, while 2x2 object, there 7 ticks on each axes, see below screenshots)
3d plot 1x1 object:
3d plot 2x2 object:
the problem number of tick-labels lower number of ticks, therefore tick-labels moved beginning of axes.
so, how can reduce/setup number of ticks?
here code:
my_w = 2 my_h = 2 x1_list_int = [] x2_list_int = [] y1_list_int = [[],[]] y1_list_int = [[0 x in range(my_w)] y in range(my_h)] #matrix initialization in xrange(my_w): print x1_list_int.append(i*10) x2_list_int.append(i+1) in xrange(my_w): 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 mesh of positions ypos = np.arange(0,ly,1) xpos, ypos = np.meshgrid(xpos+0.25, ypos+0.25) xpos = xpos.flatten() # convert positions 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 file plt.savefig(self.picture_file_path_1) .... plt.close() # final. data clean-up
i have found solution. here is:
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)) .....
Comments
Post a Comment