我想创建 3D 直方图,但我不知道为什么 X 轴有 Y 标签,Y 轴有 X 轴。怎么了?
xAmplitudes = ([0 for i, j in zip(x, width)])
yAmplitudes = centre_y
x = np.array(xAmplitudes) #turn x,y data into numpy arrays
y = np.array(yAmplitudes)
fig = plt.figure() #create a canvas, tell matplotlib it's 3d
ax = fig.add_subplot(111, projection='3d')
#make histogram stuff - set bins - I choose 50x50 because I have a lot of data
hist, xedges, yedges = np.histogram2d(x, y, bins=(50,50))
xpos, ypos = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:])
xpos = xpos.flatten()/2.
ypos = ypos.flatten()/2.
zpos = np.zeros_like (xpos)
dx = xedges [1] - xedges [0]
dy = yedges [1] - yedges [0]
dz = hist.flatten()
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=blue, zsort='average')
plt.xlabel("X ")
plt.ylabel("Y ")
最佳答案
注意 numpy.histogram2d
的含义文档说关于输出数组:
"The bi-dimensional histogram of samples x and y. Values in x are histogrammed along the first dimension and values in y are histogrammed along the second dimension."
这意味着您想要转置结果数组,可能像
dz = hist.T.flatten()
关于python - 3D 图显示错误的轴标签(X 轴有 Y 轴名称,Y 轴有 X 轴名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55201945/