Skip to content Skip to sidebar Skip to footer

How To Plot A 3d Histogram With Matplotlib/mplot3d?

I have three arrays and I am trying to make a 3D histogram. x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7] y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50, 60] z = [105, 25, 26, 74, 39, 85, 74, 15

Solution 1:

The z array must have the same shape not of x and y but of xpos and ypos (which are of themselves the same shape). You may find this example more useful than the one you appear to be drawing from. The following code is to demonstrate the example in the first link applied to your question,

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

_x = [1, 2, 3, 2, 5, 2, 6, 8, 6, 7]
_y = [10, 10, 20, 50, 20, 20, 30, 10, 40, 50]
_xx, _yy = np.meshgrid(_x, _y)
x, y = _xx.ravel(), _yy.ravel()
_z = np.array([105, 25, 26, 74, 39, 85, 74, 153, 52, 98])

# There may be an easier way to do this, but I am not aware of it
z = np.zeros(len(x))
for i inrange(1, len(x)):
    z[i] = _z[(i*len(_z)) / len(x)]

bottom = np.zeros_like(z)
width = depth = 1

ax.bar3d(x, y, bottom, width, depth, z, shade=True)
plt.show()

enter image description here

Post a Comment for "How To Plot A 3d Histogram With Matplotlib/mplot3d?"