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?
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With