Can I plot several histograms in 3d?

落爺英雄遲暮 提交于 2019-12-06 05:32:52

问题


I'd like to plot several histograms similar to the way thesebar graphs are plotted. I've tried using the arrays returned by hist, but it seems that the bin edges are returned, so I can't use them in bar.

Does anyone have any suggestions?


回答1:


If you use np.histogram to pre-compute the histogram, as you found you'll get the hist array and the bin edges. plt.bar expects the bin centres, so calculate them with:

xs = (bins[:-1] + bins[1:])/2

To adapt the Matplotlib example:

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()



来源:https://stackoverflow.com/questions/35210337/can-i-plot-several-histograms-in-3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!