8000 Disable draw_foo methods on renderer used to estimate tight extents. · matplotlib/matplotlib@e7fac8e · GitHub
[go: up one dir, main page]

Skip to content

Commit e7fac8e

Browse files
committed
Disable draw_foo methods on renderer used to estimate tight extents.
For the pgf backend, in particular draw_image() cannot succeed because that needs actual filesystem access (to set up \includegraphics).
1 parent 4764452 commit e7fac8e

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,17 @@ def _draw(renderer): raise Done(renderer)
15101510
return figure._cachedRenderer
15111511

15121512

1513+
def _disable_renderer_draw_methods(renderer):
1514+
"""
1515+
Replace draw_foo methods on *renderer* by no-ops. This is used by the
1516+
tight-bbox-saving renderer, which needs to walk through the artist tree to
1517+
compute the tight-bbox, but for which the output file may be closed early.
1518+
"""
1519+
for meth_name in dir(RendererBase):
1520+
if meth_name.startswith("draw_"):
1521+
setattr(renderer, meth_name, lambda *args, **kwargs: None)
1522+
1523+
15131524
def _is_non_interactive_terminal_ipython(ip):
15141525
"""
15151526
Return whether we are in a a terminal IPython, but non interactive.
@@ -2059,6 +2070,7 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
20592070
self.figure,
20602071
functools.partial(
20612072
print_method, dpi=dpi, orientation=orientation))
2073+
_disable_renderer_draw_methods(renderer)
20622074
self.figure.draw(renderer)
20632075
bbox_artists = kwargs.pop("bbox_extra_artists", None)
20642076
bbox_inches = self.figure.get_tightbbox(

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,11 @@ def test_tex_restart_after_error():
291291
fig = plt.figure() # start from scratch
292292
fig.suptitle(r"this is ok")
293293
fig.savefig(BytesIO(), format="pgf")
294+
295+
296+
@needs_xelatex
297+
def test_bbox_inches_tight(tmpdir):
298+
fig, ax = plt.subplots()
299+
ax.imshow([[0, 1], [2, 3]])
300+
fig.savefig(os.path.join(tmpdir, "test.pdf"), backend="pgf",
301+
bbox_inches="tight")

0 commit comments

Comments
 (0)
0