@@ -1534,7 +1534,6 @@ def _get_renderer(figure, print_method=None):
15341534
15351535 If you need a renderer without any active draw methods use
15361536 renderer._draw_disabled to temporary patch them out at your call site.
1537-
15381537 """
15391538 # This is implemented by triggering a draw, then immediately jumping out of
15401539 # Figure.draw() by raising an exception.
@@ -1545,15 +1544,23 @@ class Done(Exception):
15451544 def _draw (renderer ): raise Done (renderer )
15461545
15471546 with cbook ._setattr_cm (figure , draw = _draw ):
1547+ orig_canvas = figure .canvas
15481548 if print_method is None :
15491549 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 } " )
15511554 try :
15521555 print_method (io .BytesIO (), dpi = figure .dpi )
15531556 except Done as exc :
15541557 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
15571564
15581565
15591566def _is_non_interactive_terminal_ipython (ip ):
0 commit comments