Skip to content

Commit 0af2455

Browse files
committed
DOC: add the API notes from Matplotlib discussion
1 parent 465a7cb commit 0af2455

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

docs/source/Api_Notes.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# API design notes
2+
3+
## Current state
4+
5+
### `fig, ax = plt.subplots() # and friends`
6+
7+
- To show during interactive use or in a script: `plt.show()` will show all figures that have been created by pyplot that have note been `plt.close()`d.
8+
- `plt.show(block=False)` will show the current figure, but continue the script.
9+
- works with `inline`
10+
- works with ipympl
11+
12+
#### Pros:
13+
14+
- easy to show all plots.
15+
16+
#### Cons:
17+
18+
- mixed in with rest of `pyplot`
19+
- too many figures open; must manually close the ones we don't want anymore or memory grows. For a large jupyter notebook with cells executed many times, this can actually be substantial.
20+
21+
### `fig = matplotlib.figure.Figure()`
22+
23+
- cannot do `fig.show()`, but can do `fig.savefig()`
24+
- does not display anything in either `inline` or `ipympl`
25+
26+
### Pros:
27+
28+
- `fig` will be garbage collected because there are no references stored in a registry
29+
30+
### Cons:
31+
32+
- no way to show the figure on a GUI backend (no promotion possible).
33+
- ugly import.
34+
35+
## Proposed changes
36+
37+
### `mpl_gui`
38+
39+
- `import matplotlib.mpl_gui as mg; fig, ax = mg.subplots()`
40+
- Can be shown, but with new `mg.show([fig])` (though singleton fig could trivially be added).
41+
- jupyter: works without `show` in `ipympl`; does _not_ (currently) work (at all) with `inline`.
42+
43+
#### Pros:
44+
45+
- no global state - garbage collection on dereferenced figures
46+
- no connection to pyplot state-based interface
47+
48+
#### Cons:
49+
50+
- New import and documentation (inertia relative to pyplot)
51+
- `mg.show()` doesn't know what figures to show, so it must be supplied a list of figures.
52+
- sometimes we create figures in loops, and assigning a different variable name to each figure to stop if from being dereferenced could be cumbersome.
53+
54+
### `mpl_gui.registry`
55+
56+
- `import matplotlib.mpl_gui.registry as mr; fig, ax = mr.subplots()`
57+
- allows `mr.show()` to be exactly the same as `plt.show`.
58+
59+
#### Pros:
60+
61+
- no connection to pyplot state-based interface
62+
- exactly same as previous pyplot interface for this type of work.
63+
64+
#### Cons:
65+
66+
- figures must be explicitly closed
67+
68+
### `mpl_gui.FigureContext`
69+
70+
This is between the two extremes, where there is no global registry, but a registry is maintained for a series of plots within a context:
71+
72+
```
73+
with mg.FigureContext() as fc:
74+
fig, ax = fc.subplots()
75+
fig, ax = fc.subplot_mosaic('AA\nBC')
76+
fig = fc.figure
77+
```
78+
79+
makes three figures and shows them in a blocking manner, and then removes the registry on completion.
80+
81+
### Top-level import?
82+
83+
The question arose as to whether these should be top level imports eg `fig, ax = mpl.subplots()` A choice would need to be made as to whether that is the registry or non-registry version of the new interface.

docs/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"matplotlib.sphinxext.plot_directive",
5252
"numpydoc",
5353
"sphinx_copybutton",
54+
"myst_parser",
5455
]
5556

5657
# Configuration options for plot_directive. See:

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
api
1515
release_history
1616
min_versions
17+
Api_Notes
1718

1819

1920
Motivation

requirements-doc.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ mpl-sphinx-theme~=3.9.0
66
numpydoc
77
sphinx
88
sphinx-copybutton
9+
myst-parser

0 commit comments

Comments
 (0)