如果您使用np.histogram
来预先计算直方图,就像您发现的那样,您将获得hist
数组和bin edges。 plt.bar
期望bin的中心位置,因此请使用以下方法进行计算:
如果您使用np.histogram
來預先計算直方图,就像您發現的那樣,您將獲得hist
數組和bin edges。 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()