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

Skip to content

Commit b4a11dd

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 b4a11dd

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 5 additions & 6 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, fmt, *, 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,10 @@ class Done(Exception):
15141514
def _draw(renderer): raise Done(renderer)
15151515

15161516
with cbook._setattr_cm(figure, draw=_draw):
1517+
print_method = getattr(
1518+
figure.canvas._get_output_canvas(fmt), f"print_{fmt}")
15171519
try:
1518-
print_method(io.BytesIO())
1520+
print_method(io.BytesIO(), dpi=figure.dpi)
15191521
except Done as exc:
15201522
renderer, = figure._cachedRenderer, = exc.args
15211523

@@ -2075,10 +2077,7 @@ def print_figure(
20752077
if bbox_inches:
20762078
if bbox_inches == "tight":
20772079
renderer = _get_renderer(
2078-
self.figure,
2079-
functools.partial(
2080-
print_method, dpi=dpi, orientation=orientation),
2081-
draw_disabled=True)
2080+
self.figure, format, draw_disabled=True)
20822081
self.figure.draw(renderer)
20832082
bbox_inches = self.figure.get_tightbbox(
20842083
renderer, bbox_extra_artists=bbox_extra_artists)

lib/matplotlib/tight_layout.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,15 @@ 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-
229-
return renderer
221+
return canvas.get_renderer()
222+
else:
223+
from . import backend_bases
224+
return backend_bases._get_renderer(
225+
fig, canvas.get_default_filetype())
230226

231227

232228
def get_subplotspec_list(axes_list, grid_spec=None):

0 commit comments

Comments
 (0)
0