如下图所示,我的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))
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。