Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2980caa

Browse files
committed
Simpler "pyplotless" use pattern.
Right now, if one does not want to use pyplot (e.g., batch use), one can do from matplotlib.figure import Figure fig = Figure(); ... <plot> ...; fig.savefig(...) but it is impossible to show() the figure interactively (i.e. pyplot cannot "adopt" the figure) e.g. for debugging. This patch makes it possible: with it, one can do fig = plt.new_figure_manager(num).canvas.figure ... <plot> ... fig.savefig() plt.show(figures=[fig]) Note that this does *not* register the figure with pyplot; in particular *fig* is garbage-collected when it goes out of scope, does not participate in gcf(); etc. So this is "pyplotless" in the sense that there is no global figure registry anymore, but pyplot still stays in charge of the GUI integration. Obviously the `plt.new_figure_manager(num).canvas.figure` expression could / should be encapsulated in a helper function, up to bikeshedding. The `num` parameter is needed as matplotlib currently uses it to set the figure title, but that can easily be made optional too. Finally note that `plt.show([fig])` would be a nicer API, but right now the first parameter to plt.show is `block`, which is undergoing transition to kwonly. IOW the end-goal would be e.g. # intentionally terrible name as placeholder :) fig = plt.new_figure_not_globally_registered("some title") ... <plot> ... fig.savefig() plt.show([fig])
1 parent c05537d commit 2980caa

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,16 +2300,18 @@ def key_press_handler(event, canvas, toolbar=None):
23002300
toggle_xscale_keys = rcParams['keymap.xscale']
23012301
all_keys = rcParams['keymap.all_axes']
23022302

2303-
# toggle fullscreen mode ('f', 'ctrl + f')
2304-
if event.key in fullscreen_keys:
2305-
try:
2306-
canvas.manager.full_screen_toggle()
2307-
except AttributeError:
2308-
pass
2303+
manager = canvas.manager
23092304

2310-
# quit the figure (default key 'ctrl+w')
2311-
if event.key in quit_keys:
2312-
Gcf.destroy_fig(canvas.figure)
2305+
if manager is not None:
2306+
# toggle fullscreen mode ('f', 'ctrl + f')
2307+
if event.key in fullscreen_keys:
2308+
canvas.manager.full_screen_toggle()
2309+
# quit the figure (default key 'ctrl+w')
2310+
if event.key in quit_keys:
2311+
if Gcf.get_fig_manager(canvas.manager.num):
2312+
Gcf.destroy_fig(canvas.figure)
2313+
else:
2314+
manager.destroy()
23132315

23142316
if toolbar is not None:
23152317
# home or reset mnemonic (default key 'h', 'home' and 'r')
@@ -3264,15 +3266,16 @@ def draw_if_interactive(cls):
32643266

32653267
@classmethod
32663268
@cbook._make_keyword_only("3.1", "block")
3267-
def show(cls, block=None):
3269+
def show(cls, block=None, *, figures=None):
32683270
"""
32693271
Show all figures.
32703272
32713273
`show` blocks by calling `mainloop` if *block* is ``True``, or if it
32723274
is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in
32733275
`interactive` mode.
32743276
"""
3275-
managers = Gcf.get_all_fig_managers()
3277+
managers = ([figure.canvas.manager for figure in figures]
3278+
if figures is not None else Gcf.get_all_fig_managers())
32763279
if not managers:
32773280
return
32783281
for manager in managers:

0 commit comments

Comments
 (0)