Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 I can't use them in bar.

Does anyone have any suggestions?

like image 690
Demetri Pananos Avatar asked Feb 04 '16 19:02

Demetri Pananos


1 Answers

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

enter image description here

like image 62
xnx Avatar answered Oct 06 '22 19:10

xnx