开发者社区> 问答> 正文

在python中的3d图中更改刻度线的位置

如下图所示,我的x轴和y轴刻度线有问题。下面列出了创建此3D图的代码。我想将x轴和y轴刻度都移动到它们与图中3D条对齐的位置。如果有人可以提供帮助,我将非常感激。

from mpl_toolkits.mplot3d import Axes3D
 import matplotlib.pyplot as plt
 import numpy as np
 import matplotlib.cm as cm


 # To generate some test data
 column_names = ['1', '2', '3','4', '5', '6', '7','8', '9','10','11','12','13','14','15','16','17']
 row_names = ['T','S','R','P','N','M','L','K','J','H','G','F','E','D','C','B','A']

 x = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
         1,2,3,4,5,6,7,-1,-2,-3,-4,-5,-6,-7])
 y = np.array([0,1,2,3,4,5,6,7,-1,-2,-3,-4,-5,-6,-7,
         0,0,0,0,0,0,0,0,0,0,0,0,0,0])

 XY = np.stack((x,y),axis=-1)

 def selection(XY, limitXY=[[-9,+9],[-9,+9]]):
         XY_select = []
         for elt in XY:
             if elt[0] > limitXY[0][0] and elt[0] < limitXY[0][1] and elt[1] > limitXY[1][0] and elt[1] < limitXY[1][1]:
                 XY_select.append(elt)

         return np.array(XY_select)

 XY_select = selection(XY, limitXY=[[-9,+9],[-9,+9]])


 xAmplitudes = np.array(XY_select)[:,0]#your data here
 yAmplitudes = np.array(XY_select)[:,1]#your other data here


 fig = plt.figure(figsize=(25,25)) #create a canvas, tell matplotlib it's 3d
 ax = fig.add_subplot(111, projection='3d')


 hist, xedges, yedges = np.histogram2d(x, y, bins=(17,17), range = [[-9,+9],[-9,+9]]) # you can change your bins, and the range on which to take data
 # hist is a 7X7 matrix, with the populations for each of the subspace parts.
 xpos, ypos = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:]) -(xedges[1]-xedges[0])


 xpos = xpos.flatten()/1.
 ypos = ypos.flatten()/1.
 zpos = np.zeros_like (xpos)

 dx = (xedges [1] - xedges [0])
 dy = yedges [1] - yedges [0]
 dz = hist.flatten()   

 cmap = cm.get_cmap('jet') # Get desired colormap - you can change this!
 max_height = np.max(dz)   # get range of colorbars so we can normalize
 min_height = np.min(dz)
 # scale each z to [0,1], and get their rgb values
 rgba = [cmap((k-min_height)/max_height) for k in dz] 

 surf = ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=rgba,alpha=0.5, zsort='max'
 ticksx = 2*np.arange(0.5, 18, 1)
 plt.xticks(ticksx, column_names,size=20)

 ticksy = 2*np.arange(0.5, 18, 1)
 plt.yticks(ticksy, row_names,size=20)
 plt.grid()
 ax.view_init(30, 40)
 plt.show()
 print('ax.azim {}'.format(ax.azim))
 print('ax.elev {}'.format(ax.elev))

展开
收起
几许相思几点泪 2019-12-03 16:02:55 871 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载