@@ -1534,7 +1534,6 @@ def _get_renderer(figure, print_method=None):
1534
1534
1535
1535
If you need a renderer without any active draw methods use
1536
1536
renderer._draw_disabled to temporary patch them out at your call site.
1537
-
1538
1537
"""
1539
1538
# This is implemented by triggering a draw, then immediately jumping out of
1540
1539
# Figure.draw() by raising an exception.
@@ -1545,15 +1544,23 @@ class Done(Exception):
1545
1544
def _draw (renderer ): raise Done (renderer )
1546
1545
1547
1546
with cbook ._setattr_cm (figure , draw = _draw ):
1547
+ orig_canvas = figure .canvas
1548
1548
if print_method is None :
1549
1549
fmt = figure .canvas .get_default_filetype ()
1550
- print_method = getattr (figure .canvas , f"print_{ fmt } " )
1550
+ # Even for a canvas' default output type, a canvas switch may be
1551
+ # needed, e.g. for FigureCanvasBase.
1552
+ print_method = getattr (
1553
+ figure .canvas ._get_output_canvas (None , fmt ), f"print_{ fmt } " )
1551
1554
try :
1552
1555
print_method (io .BytesIO (), dpi = figure .dpi )
1553
1556
except Done as exc :
1554
1557
renderer , = figure ._cachedRenderer , = exc .args
1555
-
1556
- return renderer
1558
+ return renderer
1559
+ else :
1560
+ raise RuntimeError (f"{ print_method } did not call Figure.draw, so "
1561
+ f"no renderer is available" )
1562
+ finally :
1563
+ figure .canvas = orig_canvas
1557
1564
1558
1565
1559
1566
def _is_non_interactive_terminal_ipython (ip ):
0 commit comments