Skip to content

Commit a8851dd

Browse files
committed
Merge pull request #6260 from discardthree/bug-docs-1702
Bug fix and general touch ups for hist3d_demo example closes #1702
1 parent 860fdfe commit a8851dd

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

examples/mplot3d/hist3d_demo.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1+
'''
2+
Demo of a histogram for 2 dimensional data as a bar graph in 3D.
3+
'''
4+
15
from mpl_toolkits.mplot3d import Axes3D
26
import matplotlib.pyplot as plt
37
import numpy as np
48

59
fig = plt.figure()
610
ax = fig.add_subplot(111, projection='3d')
711
x, y = np.random.rand(2, 100) * 4
8-
hist, xedges, yedges = np.histogram2d(x, y, bins=4)
12+
hist, xedges, yedges = np.histogram2d(x, y, bins=4, range=[[0, 4], [0, 4]])
913

10-
elements = (len(xedges) - 1) * (len(yedges) - 1)
14+
# Construct arrays for the anchor positions of the 16 bars.
15+
# Note: np.meshgrid gives arrays in (ny, nx) so we use 'F' to flatten xpos,
16+
# ypos in column-major order. For numpy >= 1.7, we could instead call meshgrid
17+
# with indexing='ij'.
1118
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25)
19+
xpos = xpos.flatten('F')
20+
ypos = ypos.flatten('F')
21+
zpos = np.zeros_like(xpos)
1222

13-
xpos = xpos.flatten()
14-
ypos = ypos.flatten()
15-
zpos = np.zeros(elements)
23+
# Construct arrays with the dimensions for the 16 bars.
1624
dx = 0.5 * np.ones_like(zpos)
1725
dy = dx.copy()
1826
dz = hist.flatten()

0 commit comments

Comments
 (0)