|
| 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. |
0 commit comments