8000 Don't fallback to agg in tight_layout.get_renderer. · matplotlib/matplotlib@fe8560b · GitHub
[go: up one dir, main page]

Skip to content

Commit fe8560b

Browse files
committed
Don't fallback to agg in tight_layout.get_renderer.
tight_layout.get_renderer currently falls back to agg if it is unable to get a renderer for the current canvas, but we actually have a way to coerce the canvas to produce a renderer, via backend_bases._get_renderer. This renderer gets used by the tight_layout machinery to estimate text size. Falling back to Agg would produce bad results in the (admittedly rare) cases where text size estimation yields very different results depending on the renderer -- the easiest way to trigger this is to use a "thin" font, e.g. Tex Gyre Chorus, while using the pdf backend with the "pdf.use14corefonts" rcParam set to True, which effectively forces Helvetica. e.g. ``` from pylab import * matplotlib.use("pdf") rcdefaults() rcParams.update({"pdf.use14corefonts": True, "font.family": "TeX Gyre Chorus"}) plot() margins(0) tight_layout(0) savefig("/tmp/test.pdf") ``` would crop the tick labels before this PR. (The real point is not to fix an obscure edge case, but actually to have fewer places that fall back to Agg as "favorited" backend.)
1 parent 439a122 commit fe8560b

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ def __init__(self, name, canvas, key, x=0, y=0, guiEvent=None):
14951495
self.key = key
14961496

14971497

1498-
def _get_renderer(figure, print_method, *, draw_disabled=False):
1498+
def _get_renderer(figure, print_method=None, *, draw_disabled=False):
14991499
"""
15001500
Get the renderer that would be used to save a `~.Figure`, and cache it on
15011501
the figure.
@@ -1514,8 +1514,11 @@ class Done(Exception):
15141514
def _draw(renderer): raise Done(renderer)
15151515

15161516
with cbook._setattr_cm(figure, draw=_draw):
1517+
if print_method is None:
1518+
fmt = figure.canvas.get_default_filetype()
1519+
print_method = getattr(figure.canvas, f"print_{fmt}")
15171520
try:
1518-
print_method(io.BytesIO())
1521+
print_method(io.BytesIO(), dpi=figure.dpi)
15191522
except Done as exc:
15201523
renderer, = figure._cachedRenderer, = exc.args
15211524

@@ -2077,7 +2080,7 @@ def print_figure(
20772080
renderer = _get_renderer(
20782081
self.figure,
20792082
functools.partial(
2080-
print_method, dpi=dpi, orientation=orientation),
2083+
print_method, orientation=orientation),
20812084
draw_disabled=True)
20822085
self.figure.draw(renderer)
20832086
bbox_inches = self.figure.get_tightbbox(

lib/matplotlib/tight_layout.py

Lines changed: 5 additions & 10 deletions
229
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,14 @@ def auto_adjust_subplotpars(
214214

215215
def get_renderer(fig):
216216
if fig._cachedRenderer:
217-
renderer = fig._cachedRenderer
217+
return fig._cachedRenderer
218218
else:
219219
canvas = fig.canvas
220-
221220
if canvas and hasattr(canvas, "get_renderer"):
222-
renderer = canvas.get_renderer()
223-
else: # Some noninteractive backends have no renderer until draw time.
224-
cbook._warn_external("tight_layout: falling back to Agg renderer")
225-
from matplotlib.backends.backend_agg import FigureCanvasAgg
226-
canvas = FigureCanvasAgg(fig)
227-
renderer = canvas.get_renderer()
228-
-
return renderer
221+
return canvas.get_renderer()
222+
else:
223+
from . import backend_bases
224+
return backend_bases._get_renderer(fig, draw_disabled=True)
230225

231226

232227
def get_subplotspec_list(axes_list, grid_spec=None):

0 commit comments

Comments
 (0)
0