Skip to content

Commit 0cf3974

Browse files
authored
Merge pull request #17731 from anntzer/template
"Fix" tight_layout for template backend.
2 parents 63893df + 5f6f855 commit 0cf3974

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

15591566
def _is_non_interactive_terminal_ipython(ip):

lib/matplotlib/backends/backend_template.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def print_foo(self, filename, *args, **kwargs):
208208
to their original values after this call, so you don't need to
209209
save and restore them.
210210
"""
211+
self.draw()
211212

212213
def get_default_filetype(self):
213214
return 'foo'

0 commit comments

Comments
 (0)