You are reading an old version of the documentation (v1.4.2). For the latest version see https://matplotlib.org/stable/
matplotlib

Previous topic

mplot3d example code: contourf3d_demo2.py

Next topic

mplot3d example code: lines3d_demo.py

This Page

mplot3d example code: hist3d_demo.pyΒΆ

(Source code, png, hires.png, pdf)

../../_images/hist3d_demo.png
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, y = np.random.rand(2, 100) * 4
hist, xedges, yedges = np.histogram2d(x, y, bins=4)

elements = (len(xedges) - 1) * (len(yedges) - 1)
xpos, ypos = np.meshgrid(xedges[:-1]+0.25, yedges[:-1]+0.25)

xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros(elements)
dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = hist.flatten()

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color='b', zsort='average')

plt.show()

Keywords: python, matplotlib, pylab, example, codex (see Search examples)