1 | import matplotlib.pylab as pylab |
---|
2 | import matplotlib.axes3d as axes3d |
---|
3 | from matplotlib.axes3d import Axes3D |
---|
4 | |
---|
5 | # Read this: http://www.scipy.org/Cookbook/Matplotlib/mplot3D |
---|
6 | |
---|
7 | import random |
---|
8 | import numpy as npy |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | def test_scatter(): |
---|
13 | |
---|
14 | fig=pylab.figure() |
---|
15 | ax = Axes3D(fig) |
---|
16 | # |
---|
17 | # |
---|
18 | n = 100 |
---|
19 | for c,zl,zh in [('r',-50,-25),('b',-30,-5)]: |
---|
20 | xs,ys,zs = zip(* |
---|
21 | [(random.randrange(23,32), |
---|
22 | random.randrange(100), |
---|
23 | random.randrange(zl,zh) |
---|
24 | ) for i in range(n)]) |
---|
25 | ax.scatter3D(xs,ys,zs, c=c) |
---|
26 | # |
---|
27 | ax.set_xlabel('------------ X Label --------------------') |
---|
28 | ax.set_ylabel('------------ Y Label --------------------') |
---|
29 | ax.set_zlabel('------------ Z Label --------------------') |
---|
30 | |
---|
31 | def get_test_data(delta=0.05): |
---|
32 | from matplotlib.mlab import meshgrid, bivariate_normal |
---|
33 | x = y = npy.arange(-3.0, 3.0, delta) |
---|
34 | X, Y = meshgrid(x,y) |
---|
35 | |
---|
36 | Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) |
---|
37 | Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) |
---|
38 | Z = Z2-Z1 |
---|
39 | |
---|
40 | X = X * 10 |
---|
41 | Y = Y * 10 |
---|
42 | Z = Z * 500 |
---|
43 | return X,Y,Z |
---|
44 | |
---|
45 | def test_wire(): |
---|
46 | fig=pylab.figure() |
---|
47 | ax = Axes3D(fig) |
---|
48 | |
---|
49 | X,Y,Z = get_test_data(0.05) |
---|
50 | ax.plot_wireframe(X,Y,Z, rstride=10,cstride=10) |
---|
51 | # |
---|
52 | ax.set_xlabel('X') |
---|
53 | ax.set_ylabel('Y') |
---|
54 | ax.set_zlabel('Z') |
---|
55 | |
---|
56 | def test_surface(): |
---|
57 | fig=pylab.figure() |
---|
58 | ax = Axes3D(fig) |
---|
59 | |
---|
60 | X,Y,Z = get_test_data(0.05) |
---|
61 | ax.plot_surface(X,Y,Z, rstride=10,cstride=10) |
---|
62 | # |
---|
63 | ax.set_xlabel('X') |
---|
64 | ax.set_ylabel('Y') |
---|
65 | ax.set_zlabel('Z') |
---|
66 | |
---|
67 | |
---|
68 | def test_contour(): |
---|
69 | fig=pylab.figure() |
---|
70 | ax = Axes3D(fig) |
---|
71 | |
---|
72 | X,Y,Z = get_test_data(0.05) |
---|
73 | cset = ax.contour3D(X,Y,Z) |
---|
74 | ax.clabel(cset, fontsize=9, inline=1) |
---|
75 | # |
---|
76 | ax.set_xlabel('X') |
---|
77 | ax.set_ylabel('Y') |
---|
78 | ax.set_zlabel('Z') |
---|
79 | |
---|
80 | def test_plot(): |
---|
81 | fig=pylab.figure() |
---|
82 | ax = Axes3D(fig) |
---|
83 | xs = npy.arange(0,4*npy.pi+0.1,0.1) |
---|
84 | ys = npy.sin(xs) |
---|
85 | ax.plot(xs,ys, label='zl') |
---|
86 | ax.plot(xs,ys+max(xs),label='zh') |
---|
87 | ax.plot(xs,ys,dir='x', label='xl') |
---|
88 | ax.plot(xs,ys,dir='x', z=max(xs),label='xh') |
---|
89 | ax.plot(xs,ys,dir='y', label='yl') |
---|
90 | ax.plot(xs,ys,dir='y', z=max(xs), label='yh') |
---|
91 | ax.set_xlabel('X') |
---|
92 | ax.set_ylabel('Y') |
---|
93 | ax.set_zlabel('Z') |
---|
94 | ax.legend() |
---|
95 | |
---|
96 | |
---|
97 | def test_polys(): |
---|
98 | from matplotlib.collections import LineCollection, PolyCollection |
---|
99 | from matplotlib.colors import colorConverter |
---|
100 | |
---|
101 | cc = lambda arg: colorConverter.to_rgba(arg, alpha=0.6) |
---|
102 | |
---|
103 | fig=pylab.figure() |
---|
104 | ax = Axes3D(fig) |
---|
105 | xs = npy.arange(0,10,0.4) |
---|
106 | verts = [] |
---|
107 | zs = [0.0,1.0,2.0,3.0] |
---|
108 | for z in zs: |
---|
109 | ys = [random.random() for x in xs] |
---|
110 | ys[0],ys[-1] = 0,0 |
---|
111 | verts.append(zip(xs,ys)) |
---|
112 | |
---|
113 | poly = PolyCollection(verts, facecolors = [cc('r'),cc('g'),cc('b'), |
---|
114 | cc('y')]) |
---|
115 | #patches = art3d.Poly3DCollectionW(poly, zs=zs, dir='y') |
---|
116 | #poly = PolyCollection(verts) |
---|
117 | ax.add_collection(poly,zs=zs,dir='y') |
---|
118 | #ax.wrapped.add_collection(poly) |
---|
119 | # |
---|
120 | ax.plot(xs,ys, z=z, dir='y', c='r') |
---|
121 | ax.set_xlim(0,10) |
---|
122 | ax.set_ylim(-1,4) |
---|
123 | ax.set_zlim(0,1) |
---|
124 | |
---|
125 | def test_scatter2D(): |
---|
126 | xs = [random.random() for i in range(20)] |
---|
127 | ys = [random.random() for x in xs] |
---|
128 | fig=pylab.figure() |
---|
129 | ax = Axes3D(fig) |
---|
130 | ax.scatter(xs,ys) |
---|
131 | ax.scatter(xs,ys, dir='y', c='r') |
---|
132 | ax.scatter(xs,ys, dir='x', c='g') |
---|
133 | |
---|
134 | def test_bar2D(): |
---|
135 | fig=pylab.figure() |
---|
136 | ax = Axes3D(fig) |
---|
137 | |
---|
138 | for c,z in zip(['r','g','b','y'],[30,20,10,0]): |
---|
139 | xs = npy.arange(20) |
---|
140 | ys = [random.random() for x in xs] |
---|
141 | ax.bar(xs,ys,z=z,dir='y',color=c) |
---|
142 | #ax.plot(xs,ys) |
---|
143 | |
---|
144 | if __name__ == "__main__": |
---|
145 | |
---|
146 | #test_scatter() |
---|
147 | test_wire() |
---|
148 | #test_surface() |
---|
149 | #test_contour() |
---|
150 | #test_plot() |
---|
151 | #test_polys() |
---|
152 | #test_scatter2D() |
---|
153 | #test_bar2D() |
---|
154 | pylab.show() |
---|