Skip to content

Commit 295f9c0

Browse files
authored
Merge pull request #20399 from timhoffm/polys3d
Improve example for 3D polygons
2 parents d156771 + 127b642 commit 295f9c0

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

examples/mplot3d/polys3d.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,33 @@
1111
from matplotlib.collections import PolyCollection
1212
import matplotlib.pyplot as plt
1313
import numpy as np
14+
from scipy.stats import poisson
1415

1516
# Fixing random state for reproducibility
1617
np.random.seed(19680801)
1718

1819

19-
def polygon_under_graph(xlist, ylist):
20+
def polygon_under_graph(x, y):
2021
"""
2122
Construct the vertex list which defines the polygon filling the space under
22-
the (xlist, ylist) line graph. Assumes the xs are in ascending order.
23+
the (x, y) line graph. This assumes x is in ascending order.
2324
"""
24-
return [(xlist[0], 0.), *zip(xlist, ylist), (xlist[-1], 0.)]
25+
return [(x[0], 0.), *zip(x, y), (x[-1], 0.)]
2526

2627

2728
ax = plt.figure().add_subplot(projection='3d')
2829

29-
# Make verts a list such that verts[i] is a list of (x, y) pairs defining
30-
# polygon i.
31-
verts = []
30+
x = np.linspace(0., 10., 31)
31+
lambdas = range(1, 9)
3232

33-
# Set up the x sequence
34-
xs = np.linspace(0., 10., 26)
33+
# verts[i] is a list of (x, y) pairs defining polygon i.
34+
verts = [polygon_under_graph(x, poisson.pmf(l, x)) for l in lambdas]
35+
facecolors = plt.get_cmap('viridis_r')(np.linspace(0, 1, len(verts)))
3536

36-
# The ith polygon will appear on the plane y = zs[i]
37-
zs = range(4)
37+
poly = PolyCollection(verts, facecolors=facecolors, alpha=.7)
38+
ax.add_collection3d(poly, zs=lambdas, zdir='y')
3839

39-
for i in zs:
40-
ys = np.random.rand(len(xs))
41-
verts.append(polygon_under_graph(xs, ys))
42-
43-
poly = PolyCollection(verts, facecolors=['r', 'g', 'b', 'y'], alpha=.6)
44-
ax.add_collection3d(poly, zs=zs, zdir='y')
45-
46-
ax.set_xlabel('X')
47-
ax.set_ylabel('Y')
48-
ax.set_zlabel('Z')
49-
ax.set_xlim(0, 10)
50-
ax.set_ylim(-1, 4)
51-
ax.set_zlim(0, 1)
40+
ax.set(xlim=(0, 10), ylim=(1, 9), zlim=(0, 0.35),
41+
xlabel='x', ylabel=r'$\lambda$', zlabel='probability')
5242

5343
plt.show()

0 commit comments

Comments
 (0)