Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1023886

Browse files
committed
support tight_bbox for pgf output, fixes matplotlib#2342
1 parent 04b8314 commit 1023886

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
@@ -392,7 +392,7 @@ def get_width_height_descent(self, text, prop):
392392

393393
class RendererPgf(RendererBase):
394394

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

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

@@ -743,6 +750,11 @@ def get_default_filetype(self):
743750
return 'pdf'
744751

745752
def _print_pgf_to_fh(self, fh, *args, **kwargs):
753+
if kwargs.get("dryrun", False):
754+
renderer = RendererPgf(self.figure, None, dummy=True)
755+
self.figure.draw(renderer)
756+
return
757+
746758
header_text = """%% Creator: Matplotlib, PGF backend
747759
%%
748760
%% To include the figure in your LaTeX document, write
@@ -799,6 +811,7 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
799811
rendered in latex documents.
800812
"""
801813
if kwargs.get("dryrun", False):
814+
self._print_pgf_to_fh(None, *args, **kwargs)
802815
return
803816

804817
# figure out where the pgf is to be written to
@@ -861,6 +874,10 @@ def print_pdf(self, fname_or_fh, *args, **kwargs):
861874
"""
862875
Use LaTeX to compile a Pgf generated figure to PDF.
863876
"""
877+
if kwargs.get("dryrun", False):
878+
self._print_pgf_to_fh(None, *args, **kwargs)
879+
return
880+
864881
# figure out where the pdf is to be written to
865882
if is_string_like(fname_or_fh):
866883
with open(fname_or_fh, "wb") as fh:
@@ -894,6 +911,10 @@ def print_png(self, fname_or_fh, *args, **kwargs):
894911
"""
895912
Use LaTeX to compile a pgf figure to pdf and convert it to png.
896913
"""
914+
if kwargs.get("dryrun", False):
915+
self._print_pgf_to_fh(None, *args, **kwargs)
916+
return
917+
897918
if is_string_like(fname_or_fh):
898919
with open(fname_or_fh, "wb") as fh:
899920
self._print_png_to_fh(fh, *args, **kwargs)
@@ -903,7 +924,7 @@ def print_png(self, fname_or_fh, *args, **kwargs):
903924
raise ValueError("filename must be a path or a file-like object")
904925

905926
def get_renderer(self):
906-
return RendererPgf(self.figure, None)
927+
return RendererPgf(self.figure, None, dummy=True)
907928

908929

909930
class FigureManagerPgf(FigureManagerBase):

lib/matplotlib/tight_bbox.py

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

0 commit comments

Comments
 (0)