Skip to content

Commit 827cf8b

Browse files
ksundenrossbar
andauthored
Use the OO api for mpl examples (#30)
* Use the OO api for mpl examples * Apply suggestions from code review --------- Co-authored-by: Ross Barnowski <rossbar@caltech.edu>
1 parent fef8c5b commit 827cf8b

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

tutorials/matplotlib/interactive_mpl.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ If we do nothing else, this will display a snapshot of the currently-blank canva
2828
:tags: [hide-output]
2929
import matplotlib.pyplot as plt
3030

31-
32-
plt.plot([1,2,3])
31+
fig, ax = plt.subplots()
32+
ax.plot([1, 2, 3])
3333
```
3434
````
3535

@@ -38,28 +38,28 @@ plt.plot([1,2,3])
3838
3939
import matplotlib.pyplot as plt
4040
41-
plt.figure()
41+
fig, ax = plt.subplots()
4242
```
4343

4444
Plotting data to an existing figure updates the original interactive canvas in Jupyter Lab. Users can scroll up to pan and zoom.
4545

46-
To show an updated snapshot in the rendered HTML documentation, we should call `plt.gcf()` to display the current Figure.
46+
To show an updated snapshot in the rendered HTML documentation, we should place a reference to our figure, `fig`, on the last line of the cell to display the current Figure.
4747

4848
```{caution}
49-
If you re-render the canvas---such as by displaying `plt.gcf().canvas`---that will cause the cached snapshot of the figures above to update to show the latest version of the figure, ruining the sequential narrative in the rendered HTML documentation.
49+
If you re-render the canvas---such as by displaying `fig.canvas`---that will cause the cached snapshot of the figures above to update to show the latest version of the figure, ruining the sequential narrative in the rendered HTML documentation.
5050
51-
This is due to a detail of the matplotlib--Jupyter interaction. Just know to use `plt.gcf()` to safely show snapshots.
51+
This is due to a detail of the matplotlib--Jupyter interaction. Just know to use `fig` to safely show snapshots.
5252
```
5353

5454
```{code-cell} ipython3
55-
plt.plot([1, 2, 3, 4])
55+
ax.plot([1, 2, 3, 4])
5656
```
5757

5858
```{code-cell} ipython3
59-
plt.gcf()
59+
fig
6060
```
6161

6262
```{code-cell} ipython3
63-
plt.plot([1, 4, 9, 16])
64-
plt.gcf()
63+
ax.plot([1, 4, 9, 16])
64+
fig
6565
```

tutorials/matplotlib/static_mpl.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ This disables any interactivity, displaying only a PNG image of the figure in th
2424
```{code-cell} ipython3
2525
import matplotlib.pyplot as plt
2626
27-
plt.plot([1,2,3,4])
27+
fig, ax = plt.subplots()
28+
ax.plot([1, 2, 3, 4])
2829
```
2930

3031

3132
```{code-cell} ipython3
3233
import matplotlib.pyplot as plt
3334
34-
plt.plot([1,4,9,16])
35+
fig, ax = plt.subplots()
36+
ax.plot([1, 4, 9, 16])
3537
```

0 commit comments

Comments
 (0)