Skip to content Skip to sidebar Skip to footer

Can I Plot Several Histograms In 3d?

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

Solution 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 = 50for c, z inzip(['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()

enter image description here

Post a Comment for "Can I Plot Several Histograms In 3d?"