2016-09-07 41 views
0

當我繪製Axes3D對象時,X/Y軸上的刻度數自己以某種方式改變(例如,對於1x1對象,每個對象上有五個刻度軸,而對於2×2對象,則對各軸7個蜱,見下文截圖)如何減少Axes3D對象(在Matplotlib中)中的刻度數

3D繪圖爲1x1的對象: 3D plot for 1x1 object

3D繪圖爲2×2的對象: 3D plot for 2x2 object

的問題是我的勾號標籤的數量低於滴答的數量請將所有刻度標籤移至軸的開頭。

那麼,我該如何減少/設置刻度數?

這裏是我的代碼:

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

我已經找到了解決方案。這裏是:

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))  
.....