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

Skip to content

Commit cfd768e

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 5ba3911 commit cfd768e

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,19 +2485,23 @@ def key_press_handler(event, canvas=None, toolbar=None):
24852485
toggle_xscale_keys = rcParams['keymap.xscale']
24862486
all_keys = dict.__getitem__(rcParams, 'keymap.all_axes')
24872487

2488-
# toggle fullscreen mode ('f', 'ctrl + f')
2489-
if event.key in fullscreen_keys:
2490-
try:
2491-
canvas.manager.full_screen_toggle()
2492-
except AttributeError:
2493-
pass
2488+
manager = canvas.manager
24942489

2495-
# quit the figure (default key 'ctrl+w')
2496-
if event.key in quit_keys:
2497-
Gcf.destroy_fig(canvas.figure)
2490+
# quit all figures (no default)
24982491
if event.key in quit_all_keys:
24992492
Gcf.destroy_all()
25002493

2494+
if manager is not None:
2495+
# toggle fullscreen mode ('f', 'ctrl + f')
2496+
if event.key in fullscreen_keys:
2497+
canvas.manager.full_screen_toggle()
2498+
# quit the figure (default key 'ctrl+w')
2499+
if event.key in quit_keys:
2500+
if Gcf.get_fig_manager(canvas.manager.num):
2501+
Gcf.destroy_fig(canvas.figure)
2502+
else:
2503+
manager.destroy()
2504+
25012505
if toolbar is not None:
25022506
# home or reset mnemonic (default key 'h', 'home' and 'r')
25032507
if event.key in home_keys:
@@ -3545,15 +3549,16 @@ def draw_if_interactive(cls):
35453549
cls.trigger_manager_draw(manager)
35463550

35473551
@classmethod
3548-
def show(cls, *, block=None):
3552+
def show(cls, figures=None, *, block=None):
35493553
"""
3550-
Show all figures.
3554+
Show the *figures*, defaulting to all pyplot-managed figures.
35513555
35523556
`show` blocks by calling `mainloop` if *block* is ``True``, or if it
35533557
is ``None`` and we are neither in IPython's ``%pylab`` mode, nor in
35543558
`interactive` mode.
35553559
"""
3556-
managers = Gcf.get_all_fig_managers()
3560+
managers = ([figure.canvas.manager for figure in figures]
3561+
if figures is not None else Gcf.get_all_fig_managers())
35573562
if not managers:
35583563
return
35593564
for manager in managers:

0 commit comments

Comments
 (0)