Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53b088c

Browse files
committed
Tweaks to mplot3d.polys3d_demo example: change the functionality by adding extra zero vertices instead of zeroing out the first and last elements of the random data, plus reorganization/refactoring and comments.
1 parent e552b5e commit 53b088c

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

examples/mplot3d/polys3d_demo.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
1+
'''
2+
Demonstrate how to create semi-transparent polygons which fill the space
3+
under a line graph, creating a sort of 'jagged stained glass' effect.
4+
'''
5+
16
from mpl_toolkits.mplot3d import Axes3D
27
from matplotlib.collections import PolyCollection
38
from matplotlib.colors import colorConverter
49
import matplotlib.pyplot as plt
510
import numpy as np
611

712

13+
def cc(arg):
14+
'''
15+
Shorthand to convert 'named' colours to rgba format at 60% opacity.
16+
'''
17+
return colorConverter.to_rgba(arg, alpha=0.6)
18+
19+
20+
def polygon_under_graph(xlist, ylist):
21+
'''
22+
Construct the vertex list which defines the polygon filling the space under
23+
the (xlist, ylist) line graph. Assumes the xs are in ascending order.
24+
'''
25+
return [(xlist[0], 0.)] + list(zip(xlist, ylist)) + [(xlist[-1], 0.)]
26+
27+
828
fig = plt.figure()
929
ax = fig.gca(projection='3d')
1030

31+
# Make verts a list, verts[i] will be a list of (x,y) pairs defining polygon i
32+
verts = []
33+
34+
# Set up the x sequence
35+
xs = np.linspace(0., 10., 26)
1136

12-
def cc(arg):
13-
return colorConverter.to_rgba(arg, alpha=0.6)
37+
# The ith polygon will appear on the plane y = zs[i]
38+
zs = range(4)
1439

15-
xs = np.arange(0, 10, 0.4)
16-
verts = []
17-
zs = [0.0, 1.0, 2.0, 3.0]
18-
for z in zs:
40+
for i in zs:
1941
ys = np.random.rand(len(xs))
20-
ys[0], ys[-1] = 0, 0
21-
verts.append(list(zip(xs, ys)))
42+
verts.append(polygon_under_graph(xs, ys))
2243

23-
poly = PolyCollection(verts, facecolors=[cc('r'), cc('g'), cc('b'),
24-
cc('y')])
25-
poly.set_alpha(0.7)
44+
poly = PolyCollection(verts, facecolors=[cc('r'), cc('g'), cc('b'), cc('y')])
2645
ax.add_collection3d(poly, zs=zs, zdir='y')
2746

2847
ax.set_xlabel('X')
29-
ax.set_xlim3d(0, 10)
3048
ax.set_ylabel('Y')
31-
ax.set_ylim3d(-1, 4)
3249
ax.set_zlabel('Z')
50+
ax.set_xlim3d(0, 10)
51+
ax.set_ylim3d(-1, 4)
3352
ax.set_zlim3d(0, 1)
3453

3554
plt.show()

0 commit comments

Comments
 (0)