|
1 | 1 | #!/usr/bin/python |
2 | 2 |
|
3 | 3 | import numpy as np |
4 | | -from scipy.constants import pi |
5 | 4 | import time |
6 | 5 | import sys |
7 | 6 |
|
|
11 | 10 | x = np.arange(21) - 10 |
12 | 11 |
|
13 | 12 | # data I use for 3D testing |
14 | | -th = np.linspace(0, pi*2, 30) |
15 | | -ph = np.linspace(-pi/2, pi*2, 30)[:,np.newaxis] |
| 13 | +th = np.linspace(0, np.pi*2, 30) |
| 14 | +ph = np.linspace(-np.pi/2, np.pi*2, 30)[:,np.newaxis] |
16 | 15 |
|
17 | 16 | x_3d = (np.cos(ph) * np.cos(th)) .ravel() |
18 | 17 | y_3d = (np.cos(ph) * np.sin(th)) .ravel() |
|
38 | 37 | time.sleep(sleep_interval) |
39 | 38 | gp.plot((-x, x**3, {'with': 'lines'}), (x**2,)) |
40 | 39 | time.sleep(sleep_interval) |
| 40 | +gp.plot( x, np.vstack((x**3, x**2)) ) |
| 41 | +time.sleep(sleep_interval) |
| 42 | +gp.plot( np.vstack((-x**3, x**2)), _with='lines' ) |
| 43 | +time.sleep(sleep_interval) |
| 44 | +gp.plot( (np.vstack((x**3, -x**2)), {'with': 'points'} )) |
| 45 | +time.sleep(sleep_interval) |
41 | 46 |
|
42 | 47 | ################################# |
43 | 48 | # some more varied plotting, using the object-oriented interface |
44 | 49 | plot1 = gp.gnuplotlib(_with = 'linespoints', |
45 | 50 | xmin = -10, |
46 | 51 | title = 'Error bars and other things') |
47 | 52 |
|
48 | | -# this had PDL threading in it. Put it back when packed broadcastable plotting |
49 | | -# is implemented |
50 | | -plot1.plot( ( x, x**2 - 300, |
| 53 | +plot1.plot( ( np.vstack((x, x*2, x*3)), x**2 - 300, |
51 | 54 | {'with': 'lines lw 4', |
52 | 55 | 'y2': True, |
53 | | - 'legend': 'a parabola'}), |
| 56 | + 'legend': 'parabolas'}), |
54 | 57 |
|
55 | 58 | (x**2 * 10, x**2/40, x**2/2, # implicit domain |
56 | 59 | {'with': 'xyerrorbars', |
57 | 60 | 'tuplesize': 4}), |
58 | 61 |
|
59 | | - (x, x**3 - 100, |
| 62 | + (x, np.vstack((x**3, x**3 - 100)), |
60 | 63 | {"with": 'lines', |
61 | | - 'legend': 'shifted cubic', |
| 64 | + 'legend': 'shifted cubics', |
62 | 65 | 'tuplesize': 2})) |
63 | 66 | time.sleep(sleep_interval) |
64 | 67 |
|
|
89 | 92 | time.sleep(sleep_interval) |
90 | 93 |
|
91 | 94 | # sphere, ellipse together |
92 | | -# put broadcasting here |
93 | | -gp.plot3d( (x_3d, y_3d, z_3d, |
94 | | - { 'legend': 'sphere'}), |
95 | | - (2*x_3d, 2*y_3d, z_3d, |
96 | | - { 'legend': 'ellipse'}), |
| 95 | +gp.plot3d( (x_3d * np.array([[1,2]]).T, |
| 96 | + y_3d * np.array([[1,2]]).T, |
| 97 | + z_3d, |
| 98 | + { 'legend': np.array(('sphere', 'ellipse'))}), |
97 | 99 |
|
98 | 100 | title = 'sphere, ellipse', |
99 | 101 | square = True, |
|
102 | 104 |
|
103 | 105 |
|
104 | 106 | # similar, written to a png |
105 | | -# put broadcasting here |
106 | | -gp.plot3d( (x_3d, y_3d, z_3d, |
107 | | - { 'legend': 'sphere'}), |
108 | | - (2*x_3d, 2*y_3d, z_3d, |
109 | | - { 'legend': 'ellipse'}), |
| 107 | +gp.plot3d( (x_3d * np.array([[1,2]]).T, |
| 108 | + y_3d * np.array([[1,2]]).T, |
| 109 | + z_3d, |
| 110 | + { 'legend': np.array(('sphere', 'ellipse'))}), |
110 | 111 |
|
111 | 112 | title = 'sphere, ellipse', |
112 | 113 | square = True, |
|
128 | 129 | time.sleep(sleep_interval) |
129 | 130 |
|
130 | 131 | # 3d, variable color, variable pointsize |
131 | | -th = np.linspace(0, 6*pi, 200) |
132 | | -z = np.linspace(0, 5, 200) |
| 132 | +th = np.linspace(0, 6*np.pi, 200) |
| 133 | +z = np.linspace(0, 5, 200) |
133 | 134 | size = 0.5 + np.abs(np.cos(th)) |
134 | 135 | color = np.sin(2*th) |
135 | 136 |
|
136 | | -gp.plot3d( ( np.cos(th), np.sin(th), z, size, color, {'legend': "spiral 1"}), |
137 | | - (-np.cos(th), -np.sin(th), z, size, color, {'legend': "spiral 2"}), |
| 137 | +gp.plot3d( ( np.cos(th) * np.array([[1,-1]]).T, |
| 138 | + np.sin(th) * np.array([[1,-1]]).T, |
| 139 | + z, size, color, { 'legend': np.array(('spiral 1', 'spiral 2'))}), |
138 | 140 |
|
139 | 141 | title = 'double helix', |
140 | 142 | tuplesize = 5, |
|
157 | 159 | x = np.linspace(0,20,100) |
158 | 160 | gp.plot( ( z, {'tuplesize': 3, |
159 | 161 | 'with': 'image'}), |
160 | | - (x, 20*np.cos(x/20 * pi/2), |
| 162 | + (x, 20*np.cos(x/20 * np.pi/2), |
161 | 163 |
|
162 | 164 | {'tuplesize': 2, |
163 | 165 | 'with': 'lines'}), |
|
198 | 200 | ascii = False) |
199 | 201 | time.sleep(sleep_interval) |
200 | 202 |
|
201 | | -# 2 3d matrix curves |
202 | | -gp.plot((x, {'tuplesize': 3, |
203 | | - 'with': 'points palette pt 7'}), |
204 | | - (z, {'tuplesize': 3, |
205 | | - 'with': 'points ps variable pt 6'}), |
| 203 | +# Using broadcasting to plot each slice with aa different style |
| 204 | +gp.plot((np.rollaxis( np.dstack((x,z)), 2,0), |
| 205 | + {'tuplesize': 3, |
| 206 | + 'with': np.array(('points palette pt 7','points ps variable pt 6'))}), |
206 | 207 |
|
207 | 208 | title = '2 3D matrix plots. Binary.', |
208 | 209 | square = 1, |
|
219 | 220 | # time.sleep(sleep_interval) |
220 | 221 |
|
221 | 222 | # 2 3d matrix curves |
222 | | -gp.plot((x, {'tuplesize': 3, |
223 | | - 'with': 'points palette pt 7'}), |
224 | | - (z, {'tuplesize': 3, |
225 | | - 'with': 'points ps variable pt 6'}), |
| 223 | +gp.plot((np.rollaxis( np.dstack((x,z)), 2,0), |
| 224 | + {'tuplesize': 3, |
| 225 | + 'with': np.array(('points palette pt 7','points ps variable pt 6'))}), |
226 | 226 |
|
227 | 227 | title = '2 3D matrix plots. Binary.', |
228 | 228 | square = 1, |
|
237 | 237 | y -= 30.0 |
238 | 238 | z = np.sin(x / 4.0) * y |
239 | 239 |
|
240 | | -# single 3d matrix curve |
241 | | -gp.plot3d( (z, {'tuplesize': 3, 'with': 'image'}), |
242 | | - (z, {'tuplesize': 3, 'with': 'lines'}), |
243 | | - |
244 | | - title = 'matrix plot with contours', |
245 | | - cmds = [ 'set contours base', |
246 | | - 'set cntrparam bspline', |
247 | | - 'set cntrparam levels 15', |
248 | | - 'unset grid', |
249 | | - 'unset surface', |
250 | | - 'set view 0,0'], |
| 240 | +# single 3d matrix curve. Two plots: the image and the contours together. |
| 241 | +# Broadcasting the styles |
| 242 | +gp.plot3d( (z, {'tuplesize': 3, 'with': np.array(('image','lines'))}), |
| 243 | + |
| 244 | + title = 'matrix plot with contours', |
| 245 | + cmds = [ 'set contours base', |
| 246 | + 'set cntrparam bspline', |
| 247 | + 'set cntrparam levels 15', |
| 248 | + 'unset grid', |
| 249 | + 'unset surface', |
| 250 | + 'set view 0,0'], |
251 | 251 | square = 1 ) |
252 | 252 | time.sleep(sleep_interval) |
253 | 253 |
|
|
261 | 261 | sys.stderr.write('I should complain about an invalid "with":\n') |
262 | 262 | sys.stderr.write("=================================\n") |
263 | 263 | try: |
264 | | - gp.plot(x, _with = 'bogusstyle') |
| 264 | + gp.plot(np.arange(5), _with = 'bogusstyle') |
265 | 265 | except gp.GnuplotlibError as e: |
266 | 266 | print("OK! Got err I was supposed to get:\n[[[[[[[\n{}\n]]]]]]]\n".format(e)) |
267 | 267 | except: |
|
0 commit comments