python - 3D 图显示错误的轴标签(X 轴有 Y 轴名称,Y 轴有 X 轴名称)

标签 python matplotlib plot

我想创建 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 ")

plot

最佳答案

注意 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/

相关文章:

matlab - 如何为不同的间隔绘制不同颜色的函数?

python - 在 Python 中绘制网络/关系/连接类型图

python - Appengine REST 库 - Python 和 Django

python - 通过 Homebrew 并行安装 Python 2.7 和 3.3 - pip3 失败

python - 使用 3 个索引对 4 维数组进行切片时会发生什么?

python - 添加补丁时 Matplotlib 动画速度变慢

r - ggplot 为每个值获取颜色

python - pycurl 相当于 "curl --data-binary"

python - 在 Matplotlib 中绘制一个 3d 立方体、一个球体和一个向量

image-processing - 使用 matplotlib 检测图像中的鼠标事件