-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-12-matplotlib.qmd
More file actions
479 lines (358 loc) · 14.8 KB
/
Copy pathpython-12-matplotlib.qmd
File metadata and controls
479 lines (358 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
---
title: "Plotting with matplotlib"
---
<!-- > Quick reminder that today we are running -->
<!-- > Python via JupyterHub on our training cluster. Point your browser to https://oc.c3.ca, log in with -->
<!-- > your username and password from last week, then launch a JupyterHub server with time = ***3 hours***, **1 CPU core**, -->
<!-- > memory = ***3600 MB***, GPU configuration = ***None***, user interface = ***JupyterLab***. Finally, start a -->
<!-- > new Python 3 notebook. -->
There are hundreds of visualization packages in Python. Check out this [diagram of the Python Visualization
Landscape](https://raw.githubusercontent.com/rougier/python-visualization-landscape/master/landscape-colors.png){target="_blank"}
(circa 2017, by Nicolas Rougier) which focuses on 1D+2D packages at the time (and only barely mentions 3D
sci-vis packages). For 3D examples, check [our gallery](https://ccvis.netlify.app){target="_blank"} in which
most images were rendered with Python.
- [Matplotlib](https://matplotlib.org){target="_blank"} - plotting into static images
- [Plotly](https://plotly.com/python){target="_blank"} - plotting into interactive HTML5
- [Bokeh](https://demo.bokeh.org){target="_blank"} - also plotting into interactive HTML5
- [plotnine](https://plotnine.org){target="_blank"} - Python clone of R's ggplot2 (based on the
"Grammar of Graphics")
One of the most widely used Python plotting libraries is matplotlib. Matplotlib is open source and produces
static images (and non-interactive animations).
## Simple line/scatter plots
If working in a Jupyter notebook, you can create a simple line plot with:
```py
%matplotlib inline
import matplotlib.pyplot as plt
plt.figure(figsize=(10,8))
from numpy import linspace, sin
x = linspace(0.01,1,300)
y = sin(1/x)
plt.plot(x, y, 'bo-')
plt.xlabel('x', fontsize=18)
plt.ylabel('f(x)', fontsize=18)
```
If working inside a terminal on your own computer (where you can open windows), you can display the graph with:
```sh
plt.show() # not needed inside the Jupyter notebook
```
Both in a Jupyter notebook and in the terminal, you can save the plot with:
```sh
plt.savefig('filename.png')
# plt.savefig('filename.png', dpi=300) # optionally specify the resolution
```
{width=85%}
> **Offscreen plotting** -
> You can create the same plot with offscreen rendering directly to a file:
> ```py
> import matplotlib as mpl
> import matplotlib.pyplot as plt
> mpl.use('Agg') # enable PNG backend
> plt.figure(figsize=(10,8))
> from numpy import linspace, sin
> x = linspace(0.01,1,300)
> y = sin(1/x)
> plt.plot(x, y, 'bo-')
> plt.xlabel('x', fontsize=18)
> plt.ylabel('f(x)', fontsize=18)
> plt.savefig('filename.png')
> ```
Let's add the second line, the labels, and the legend. Note that matplotlib automatically adjusts the axis ranges to fit
both plots:
```py
%matplotlib inline
import matplotlib.pyplot as plt
plt.figure(figsize=(10,8))
from numpy import linspace, sin
x = linspace(0.01,1,300)
y = sin(1/x)
plt.plot(x, y, 'bo-', label='one')
plt.plot(x+0.3, 2*sin(10*x), 'r-', label='two')
plt.legend(loc='lower right')
plt.xlabel('x', fontsize=18)
plt.ylabel('f(x)', fontsize=18)
```
<!-- Plotting Legendre Polynomials: -->
<!-- ```py -->
<!-- import numpy as np -->
<!-- import matplotlib.pyplot as plt -->
<!-- from scipy.special import lpmv -->
<!-- ls = [0,1,2,3] -->
<!-- x = np.linspace(-1,1,100) -->
<!-- plt.figure() -->
<!-- for l in ls: -->
<!-- plt.plot(x,lpmv(0,l,x),label=r'$l=$'+str(l)) -->
<!-- plt.title(r'Legendre Polynomials, $P_l(x)$') -->
<!-- plt.xlabel(r'$x$') -->
<!-- plt.ylabel(r'$P_l(x)$') -->
<!-- plt.legend() -->
<!-- plt.grid() -->
<!-- plt.show() -->
<!-- ``` -->
Let's plot these two functions side-by-side:
```py
%matplotlib inline
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12,4))
from numpy import linspace, sin
x = linspace(0.01,1,300)
y = sin(1/x)
ax = fig.add_subplot(121) # on 1x2 layout create plot #1 (`axes` object with some data space)
plt.plot(x, y, 'bo-', label='one')
ax.set_ylim(-1.5, 1.5)
plt.xlabel('x')
plt.ylabel('f1')
plt.legend(loc='lower right')
fig.add_subplot(122) # on 1x2 layout create plot #2
plt.plot(x+0.2, 2*sin(10*x), 'r-', label='two')
plt.xlabel('x')
plt.ylabel('f2')
plt.legend(loc='lower right')
```
There is also an option to specify absolute coordinates of each plot with `fig.add_axes()`:
1. replace the first `ax = fig.add_subplot(121)` with `ax = fig.add_axes([0.1, 0.7, 0.8, 0.3]) # left, bottom, width, height`
1. replace the second `fig.add_subplot(122)` with `fig.add_axes([0.1, 0.2, 0.8, 0.4]) # left, bottom, width, height`
The 3rd option for more fine-grained control is `plt.axes()` -- it creates an `axes` object (a region of the figure with
some data space). These two lines are equivalent - both create a new figure with one subplot:
```py
fig = plt.figure(figsize=(8,8)); ax = fig.add_subplot(111)
fig = plt.figure(figsize=(8,8)); ax = plt.axes()
```
Shortly we will see that we can pass additional flags to `fig.add_subplot()` and `plt.axes()` for more coordinate system
control.
::: {.callout-caution collapse="true"}
## Question 12.1
Break the plot into two subplots, the fist taking 1/3 of the space on the left, the second one 2/3 of the space on the
right.
:::
Let's plot a simple line in the x-y plane:
```py
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111)
x = np.linspace(0,1,100)
plt.plot(2*np.pi*x, x, 'b-')
plt.xlabel('x')
plt.ylabel('f1')
```
Replace `ax = fig.add_subplot(111)` with `ax = fig.add_subplot(111, projection='polar')`. Now we have a plot
in the phi-r plane, i.e. in polar coordinates. `Phi` goes [0,2$\pi$], whereas `r` goes [0,1].
```py
?fig.add_subplot # look into `projection` parameter
```
```py
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(12,12))
ax = fig.add_subplot(111, projection='mollweide')
lon = np.radians(np.linspace(30,90,10))
lat = np.radians(np.linspace(15,18,10))
plt.plot(lon, lat, 'go-')
```
You can use this `projection` parameter together with `cartopy` package to process 2D geospatial data to
produce maps, while all plotting is still being done by Matplotlib. We teach
[cartopy](https://scitools.org.uk/cartopy){target="_blank"} in a separate workshop.
Let's try a scatter plot:
```py
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(10,8))
x = np.random.random(size=1000) # 1D array of 1000 random numbers in [0,1]
y = np.random.random(size=1000)
size = 1 + 50*np.random.random(size=1000)
plt.scatter(x, y, s=size, color='lightblue')
```
For other plot types, click on any example in the
[Matplotlib gallery](https://matplotlib.org/gallery){target="_blank"}.
For colours, see [Choosing Colormaps in
Matplotlib](https://matplotlib.org/3.3.1/tutorials/colors/colormaps.html){target="_blank"}.
## Heatmaps
Let's plot a heatmap of monthly temperatures at the South Pole:
```py
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
plt.figure(figsize=(15,10))
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Year']
recordHigh = [-14.4,-20.6,-26.7,-27.8,-25.1,-28.8,-33.9,-32.8,-29.3,-25.1,-18.9,-12.3,-12.3]
averageHigh = [-26.0,-37.9,-49.6,-53.0,-53.6,-54.5,-55.2,-54.9,-54.4,-48.4,-36.2,-26.3,-45.8]
dailyMean = [-28.4,-40.9,-53.7,-57.8,-58.0,-58.9,-59.8,-59.7,-59.1,-51.6,-38.2,-28.0,-49.5]
averageLow = [-29.6,-43.1,-56.8,-60.9,-61.5,-62.8,-63.4,-63.2,-61.7,-54.3,-40.1,-29.1,-52.2]
recordLow = [-41.1,-58.9,-71.1,-75.0,-78.3,-82.8,-80.6,-79.3,-79.4,-72.0,-55.0,-41.1,-82.8]
vlabels = ['record high', 'average high', 'daily mean', 'average low', 'record low']
Z = np.stack((recordHigh,averageHigh,dailyMean,averageLow,recordLow))
plt.imshow(Z, cmap=cm.winter)
plt.colorbar(orientation='vertical', shrink=0.45, aspect=20)
plt.xticks(range(13), months, fontsize=15)
plt.yticks(range(5), vlabels, fontsize=12)
plt.ylim(-0.5, 4.5)
for i in range(len(months)):
for j in range(len(vlabels)):
text = plt.text(i, j, Z[j,i],
ha="center", va="center", color="w", fontsize=14, weight='bold')
```
::: {.callout-caution collapse="true"}
## Question 12.2
Change the text colour to black in the brightest (green) rows and columns. You can do this either by specifying
rows/columns explicitly, or (better) by setting a threshold background colour.
:::
::: {.callout-caution collapse="true"}
## Question 12.3
This is a take-home exercise. Modify the code to display only 4 seasons instead of the individual months.
:::
## 3D topographic elevation
For this we need a data file -- let's download it. Open a terminal inside your Jupyter dashboard. Inside the terminal, type:
```sh
wget http://bit.ly/pythfiles -O pfiles.zip
unzip pfiles.zip && rm pfiles.zip # this should unpack into the directory data-python/
```
This will download and unpack the ZIP file into your home directory. Now switch back to Python.
```py
%pwd # run `pwd` bash command
%ls # make sure you see data-python/
```
Let's plot tabulated topographic elevation data:
```py
from matplotlib import cm
from matplotlib.colors import LightSource
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
table = pd.read_csv('data-python/mt_bruno_elevation.csv')
z = np.array(table)
nrows, ncols = z.shape
x = np.linspace(0,1,ncols)
y = np.linspace(0,1,nrows)
x, y = np.meshgrid(x, y)
rgb = LightSource(270, 45).shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft')
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'), figsize=(10,10)) # figure with one subplot
ax.view_init(20, 30) # (theta, phi) viewpoint
surf = ax.plot_surface(x, y, z, facecolors=rgb, linewidth=0, antialiased=False, shade=False)
```
<!-- **Note**: If you absolutely cannot locate your downloaded data file, you can also find it in the shared folder at -->
<!-- `/project/def-sponsor00/shared/astro/data/mt_bruno_elevation.csv`. -->
::: {.callout-caution collapse="true"}
## Question 12.4
Replace `fig, ax = plt.subplots()` with `fig = plt.figure()` followed by `ax = fig.add_subplot()`. Don't
forget about the `3d` projection. This one is a little tricky -- feel free to google the problem, or even
better use our earlier examples.
:::
<!-- ```py -->
<!-- fig = plt.figure(figsize=(10,10)) -->
<!-- ax = fig.add_subplot(111, projection='3d') -->
<!-- ``` -->
Let's add the following to the previous code (running this takes ~10s on my laptop):
```py
for angle in range(90):
print(angle)
ax.view_init(20, 30+angle)
plt.savefig('frame%04d'%(angle)+'.png')
```
And then we can create a movie in bash:
```sh
ffmpeg -r 30 -i frame%04d.png -c:v libx264 -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" spin.mp4
```
## Matplotlib's built-in animation
Matplotlib can do live animation with one of its Animation classes:
#### FuncAnimation class
`FuncAnimation` creates an animation by repeatedly calling a function.
```py
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
fig = plt.figure(figsize=(8,5))
ax = plt.subplot(111)
ax.set_xlim(( 0, 2))
ax.set_ylim((-2, 2))
ax.set_xlabel('time')
ax.set_ylabel('magnitude')
# create an empty title and 2 empty plots
title = ax.set_title('')
line1 = ax.plot([], [], 'b', lw=1)[0] # `ax.plot` returns a list of 2D line objects
line2 = ax.plot([], [], 'r', lw=2)[0]
ax.legend(['sin','cos'])
def drawframe(j):
x = np.linspace(0, 2, 100)
y1 = np.sin(2 * np.pi * (x-0.01*j))
y2 = np.cos(2 * np.pi * (x-0.01*j))
line1.set_data(x, y1)
line2.set_data(x, y2)
title.set_text('frame = {0:4d}'.format(j))
return (line1,line2,title) # the animation function must return a sequence of Artist objects
# blit=True re-draws only the parts that have changed, update every 20ms, calls drawframe() with j=0..99
anim = animation.FuncAnimation(fig, drawframe, frames=100, interval=20, blit=True)
# ---
# Output option 1: Python shell, open a new window
plt.show()
# Output option 2: Jupyter notebook
from IPython.display import HTML
HTML(anim.to_html5_video())
# Output option 3: save to a file
anim.save("twoLines.mp4")
# Output option 4: save to a file, more granular control
writer = animation.FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
anim.save("twoLines.mp4", writer=writer)
```
#### ArtistAnimation class
`ArtistAnimation` creates an animation by using a fixed set of Artist objects.
```py
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import animation
fig, ax = plt.subplots()
def f(x, y):
return np.sin(x) + np.cos(y)
x = np.linspace(0, 2 * np.pi, 120)
y = np.linspace(0, 2 * np.pi, 100).reshape(-1, 1)
ims = [] # list of rows, each row is a list of artists (images) to draw in the current frame
for i in range(60):
x += np.pi / 15
y += np.pi / 30
im = ax.imshow(f(x, y), animated=True)
if i == 0:
ax.imshow(f(x, y)) # show an initial one first
ims.append([im])
# blit=True re-draws only the parts that have changed, update every 50ms
anim = animation.ArtistAnimation(fig, ims, interval=50, blit=True)
# ---
# Output option 1: Python shell, open a new window
plt.show()
# Output option 2: Jupyter notebook
from IPython.display import HTML
HTML(anim.to_html5_video())
# Output option 3: save to a file
anim.save("movingPlane.mp4")
# Output option 4: save to a file, more granular control
writer = animation.FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
anim.save("movingPlane.mp4", writer=writer)
```
## 3D parametric plot
Here is something visually very different, still using `ax.plot_surface()`:
```py
from matplotlib import cm
from matplotlib.colors import LightSource
import matplotlib.pyplot as plt
from numpy import pi, sin, cos, mgrid
dphi, dtheta = pi/250, pi/250 # 0.72 degrees
[phi, theta] = mgrid[0:pi+dphi*1.5:dphi, 0:2*pi+dtheta*1.5:dtheta]
# define two 2D grids: both phi and theta are (252,502) numpy arrays
r = sin(4*phi)**3 + cos(2*phi)**3 + sin(6*theta)**2 + cos(6*theta)**4
x = r*sin(phi)*cos(theta) # x is also (252,502)
y = r*cos(phi) # y is also (252,502)
z = r*sin(phi)*sin(theta) # z is also (252,502)
rgb = LightSource(270, 45).shade(z, cmap=cm.gist_earth, vert_exag=0.1, blend_mode='soft')
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'), figsize=(10,10))
ax.view_init(20, 30)
surf = ax.plot_surface(x, y, z, facecolors=rgb, linewidth=0, antialiased=False, shade=False)
```
::: {.callout-caution collapse="true"}
## Question 12.5
Create an animation in which you change the light source position.
:::
## More examples
For more 3D examples in matplotlib, click on any example in the [3D
gallery](https://matplotlib.org/stable/gallery/mplot3d){target="_blank"} to see the code behind that plot. Try
pasting it into your Jupyter notebook and running it, and try to modify the code.
- [Matplotlib cheatsheets and handouts](https://matplotlib.org/cheatsheets){target="_blank"}