Skip to content

Commit 3ccc17b

Browse files
committed
Always attach a manager attribute (possibly None) on canvas.
This avoids the confusion in the earlier implementation of Figure.show, which checked separately for whether the attribute existed and whether it is None, and (wrongly, I believe) handled the two cases differently.
1 parent 90fba03 commit 3ccc17b

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
``FigureCanvasBase`` now always has a ``manager`` attribute, which may be None
2+
``````````````````````````````````````````````````````````````````````````````
3+
4+
Previously, it did not necessarily have such an attribute. A check for
5+
``hasattr(figure.canvas, "manager")`` should now be replaced by
6+
``figure.canvas.manager is not None`` (or ``getattr(figure.canvas, "manager", None) is not None``
7+
for back-compatibility).

lib/matplotlib/backend_bases.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,7 @@ def __init__(self, figure):
16271627
self._is_saving = False
16281628
figure.set_canvas(self)
16291629
self.figure = figure
1630+
self.manager = None
16301631
# a dictionary from event name to a dictionary that maps cid->func
16311632
self.callbacks = cbook.CallbackRegistry()
16321633
self.widgetlock = widgets.LockDraw()
@@ -2127,7 +2128,7 @@ def get_window_title(self):
21272128
Get the title text of the window containing the figure.
21282129
Return None if there is no window (e.g., a PS backend).
21292130
"""
2130-
if hasattr(self, "manager"):
2131+
if self.manager:
21312132
return self.manager.get_window_title()
21322133

21332134
def set_window_title(self, title):

lib/matplotlib/figure.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -430,25 +430,18 @@ def show(self, warn=True):
430430
If ``True`` and we are not running headless (i.e. on Linux with an
431431
unset DISPLAY), issue warning when called on a non-GUI backend.
432432
"""
433+
if self.canvas.manager is None:
434+
raise AttributeError(
435+
"Figure.show works only for figures managed by pyplot, "
436+
"normally created by pyplot.figure()")
433437
try:
434-
manager = getattr(self.canvas, 'manager')
435-
except AttributeError as err:
436-
raise AttributeError("%s\n"
437-
"Figure.show works only "
438-
"for figures managed by pyplot, normally "
439-
"created by pyplot.figure()." % err)
440-
441-
if manager is not None:
442-
try:
443-
manager.show()
444-
return
445-
except NonGuiException:
446-
pass
447-
if (backends._get_running_interactive_framework() != "headless"
448-
and warn):
449-
cbook._warn_external('Matplotlib is currently using %s, which is '
450-
'a non-GUI backend, so cannot show the '
451-
'figure.' % get_backend())
438+
self.canvas.manager.show()
439+
except NonGuiException:
440+
if (backends._get_running_interactive_framework() != "headless"
441+
and warn):
442+
cbook._warn_external(
443+
f"Matplotlib is currently using {get_backend()}, which is "
444+
f"a non-GUI backend, so cannot show the figure.")
452445

453446
def _get_axes(self):
454447
return self._axstack.as_list()

0 commit comments

Comments
 (0)