Python 我可以在3d中绘制几个直方图吗?

Python 我可以在3d中绘制几个直方图吗?,python,matplotlib,histogram,Python,Matplotlib,Histogram,我想画几个类似于条形图的柱状图。我尝试过使用由hist返回的数组,但似乎返回了箱子边缘,因此我无法在bar中使用它们 有人有什么建议吗?如果您使用np.histogram预计算直方图,您会得到hist数组和箱子边缘plt.bar需要bin中心,因此使用以下公式计算: xs = (bins[:-1] + bins[1:])/2 要调整Matplotlib示例,请执行以下操作: from mpl_toolkits.mplot3d import Axes3D import matplotlib.py

我想画几个类似于条形图的柱状图。我尝试过使用由
hist
返回的数组,但似乎返回了箱子边缘,因此我无法在
bar
中使用它们


有人有什么建议吗?

如果您使用
np.histogram
预计算直方图,您会得到
hist
数组和箱子边缘
plt.bar
需要bin中心,因此使用以下公式计算:

xs = (bins[:-1] + bins[1:])/2
要调整Matplotlib示例,请执行以下操作:

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
nbins = 50
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
    ys = np.random.normal(loc=10, scale=10, size=2000)

    hist, bins = np.histogram(ys, bins=nbins)
    xs = (bins[:-1] + bins[1:])/2

    ax.bar(xs, hist, zs=z, zdir='y', color=c, ec=c, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()