8000 Merge pull request #2402 from pwuertz/fix_2342 · matplotlib/matplotlib@2449199 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2449199

Browse files
committed
Merge pull request #2402 from pwuertz/fix_2342
support tight_bbox for pgf output, fixes #2342 (v1.3.x)
2 parents 9b593ae + 5c411e0 commit 2449199

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def get_width_height_descent(self, text, prop):
390390

391391
class RendererPgf(RendererBase):
392392

393-
def __init__(self, figure, fh):
393+
def __init__(self, figure, fh, dummy=False):
394394
"""
395395
Creates a new PGF renderer that translates any drawing instruction
396396
into text commands to be interpreted in a latex pgfpicture environment.
@@ -408,6 +408,13 @@ def __init__(self, figure, fh):
408408
# get LatexManager instance
409409
self.latexManager = LatexManagerFactory.get_latex_manager()
410410

411+
# dummy==True deactivate all methods
412+
if dummy:
413+
nop = lambda *args, **kwargs: None
414+
for m in RendererPgf.__dict__.keys():
415+
if m.startswith("draw_"):
416+
self.__dict__[m] = nop
417+
411418
def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
412419
writeln(self.fh, r"\begin{pgfscope}")
413420

@@ -741,6 +748,11 @@ def get_default_filetype(self):
741748
return 'pdf'
742749

743750
def _print_pgf_to_fh(self, fh, *args, **kwargs):
751+
if kwargs.get("dryrun", False):
752+
renderer = RendererPgf(self.figure, None, dummy=True)
753+
self.figure.draw(renderer)
754+
return
755+
744756
header_text = r"""%% Creator: Matplotlib, PGF backend
745757
%%
746758
%% To include the figure in your LaTeX document, write
@@ -797,6 +809,7 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
797809
rendered in latex documents.
798810
"""
799811
if kwargs.get("dryrun", False):
812+
self._print_pgf_to_fh(None, *args, **kwargs)
800813
return
801814

802815
# figure out where the pgf is to be written to
@@ -859,6 +872,10 @@ def print_pdf(self, fname_or_fh, *args, **kwargs):
859872
"""
860873
Use LaTeX to compile a Pgf generated figure to PDF.
861874
"""
875+
if kwargs.get("dryrun", False):
876+
self._print_pgf_to_fh(None, *args, **kwargs)
877+
return
878+
862879
# figure out where the pdf is to be written to
863880
if is_string_like(fname_or_fh):
864881
with open(fname_or_fh, "wb") as fh:
@@ -892,6 +909,10 @@ def print_png(self, fname_or_fh, *args, **kwargs):
892909
"""
893910
Use LaTeX to compile a pgf figure to pdf and convert it to png.
894911
"""
912+
if kwargs.get("dryrun", False):
913+
self._print_pgf_to_fh(None, *args, **kwargs)
914+
return
915+
895916
if is_string_like(fname_or_fh):
896917
with open(fname_or_fh, "wb") as fh:
897918
self._print_png_to_fh(fh, *args, **kwargs)
@@ -901,7 +922,7 @@ def print_png(self, fname_or_fh, *args, **kwargs):
901922
raise ValueError("filename must be a path or a file-like object")
902923

903924
def get_renderer(self):
904-
return RendererPgf(self.figure, None)
925+
return RendererPgf( 87D4 self.figure, None, dummy=True)
905926

906927

907928
class FigureManagerPgf(FigureManagerBase):

lib/matplotlib/tight_bbox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,5 @@ def process_figure_for_rasterizing(figure,
131131
_adjust_bbox_handler_d = {}
132132
for format in ["png", "raw", "rgba", "jpg", "jpeg", "tiff"]:
133133
_adjust_bbox_handler_d[format] = adjust_bbox_png
134-
for format in ["pdf", "eps", "svg", "svgz"]:
134+
for format in ["pgf", "pdf", "eps", "svg", "svgz"]:
135135
_adjust_bbox_handler_d[format] = adjust_bbox_pdf

0 commit comments

Comments
 (0)
0