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