Using the Hist Function to Build a Series of 1D Histograms in Python 3D Graphics

I am using the pythons histogram function to generate 1d histograms, each of which is associated with this experiment. Now I understand that the Hist function allows you to display multiple histograms on the same x axis for comparison. I usually use something similar to the following for this purpose, and the result is a very good graph, where x1, x2 and x3 are defined as follows x1 = length expt1 x2 = length expt2 x3 = length 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 to achieve a 3D effect, and so I ask to what extent does anyone know if it is possible for each unique histogram to shift from another in the y plane on this module, thereby generating a 3D effect.

I would be grateful for any help with this.

+3
source share
2 answers

I believe that you just need to 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

+5
source

All Articles