user756413
user756413

Reputation: 33

Using Hist function to build series of 1D histograms in Python 3d plot

I am using pythons hist function to generate 1d histograms each linked to a given experiment. Now I understand that the Hist function allows one to plot multiple histograms on the same x axis for comparison. I normally use something akin to the following for this purpose and the result is a very nice plot where x1,x2 and x3 are defined as follows x1 = lengths expt1 x2 = lengths expt2 x3 = lengths expt3

P.figure()
n, bins, patches = P.hist( [x0,x1,x2], 10, weights=[w0, w1, w2], histtype='bar')
P.show()

I was hoping to try achieve a 3d effect however and therefore I ask dose anyone know if it is it possible to have each unique histogram offset from the other in the y plane by a given unit thus generating a 3d effect.

I would appreciate any help on this .

Upvotes: 3

Views: 1852

Answers (1)

Joe Kington
Joe Kington

Reputation: 284602

I believe you just want matplotlib.pyplot.bar3d.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x0, x1, x2 = [np.random.normal(loc=loc, size=100) for loc in [1, 2, 3]]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

yspacing = 1
for i, measurement in enumerate([x0, x1, x2]):
    hist, bin_edges = np.histogram(measurement, bins=10)
    dx = np.diff(bin_edges)
    dy = np.ones_like(hist)
    y = i * (1 + yspacing) * np.ones_like(hist)
    z = np.zeros_like(hist)
    ax.bar3d(bin_edges[:-1], y, z, dx, dy, hist, color='b', 
            zsort='average', alpha=0.5)

plt.show()

enter image description here

Upvotes: 5

Related Questions