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

Skip to content

Commit ca293ce

Browse files
pwuertzmdboom
authored andcommitted
Merge pull request #1975 from pwuertz/pgf_mixedrenderer
MixedModeRenderer non-72-dpi fixes & Pgf mixed rendering
1 parent b093b8c commit ca293ce

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

lib/matplotlib/backends/backend_mixed.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(self, figure, width, height, dpi, vector_renderer,
4848
# the figure dpi before and after the rasterization. Although
4949
# this looks ugly, I couldn't find a better solution. -JJL
5050
self.figure=figure
51+
self._figdpi = figure.get_dpi()
5152

5253
self._bbox_inches_restore = bbox_inches_restore
5354

@@ -121,16 +122,19 @@ def stop_rasterizing(self):
121122
image.is_grayscale = False
122123
image.flipud_out()
123124
gc = self._renderer.new_gc()
125+
# TODO: If the mixedmode resolution differs from the figure's
126+
# dpi, the image must be scaled (dpi->_figdpi). Not all
127+
# backends support this.
124128
self._renderer.draw_image(
125129
gc,
126-
float(l)/self.dpi*72.,
127-
(float(height) - b - h)/self.dpi*72.,
130+
float(l) / self.dpi * self._figdpi,
131+
(float(height)-b-h) / self.dpi * self._figdpi,
128132
image)
129133
self._raster_renderer = None
130134
self._rasterizing = False
131135

132-
# restore the figure dpi.
133-
self.figure.set_dpi(72)
136+
# restore the figure dpi.
137+
self.figure.set_dpi(self._figdpi)
134138

135139
if self._bbox_inches_restore: # when tight bbox is used
136140
r = process_figure_for_rasterizing(self.figure,

lib/matplotlib/backends/backend_pgf.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import matplotlib as mpl
1414
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
1515
FigureManagerBase, FigureCanvasBase
16+
from matplotlib.backends.backend_mixed import MixedModeRenderer
1617
from matplotlib.figure import Figure
1718
from matplotlib.text import Text
1819
from matplotlib.path import Path
@@ -738,7 +739,7 @@ def __init__(self, *args):
738739
def get_default_filetype(self):
739740
return 'pdf'
740741

741-
def _print_pgf_to_fh(self, fh):
742+
def _print_pgf_to_fh(self, fh, *args, **kwargs):
742743
header_text = r"""%% Creator: Matplotlib, PGF backend
743744
%%
744745
%% To include the figure in your LaTeX document, write
@@ -767,6 +768,7 @@ def _print_pgf_to_fh(self, fh):
767768

768769
# get figure size in inch
769770
w, h = self.figure.get_figwidth(), self.figure.get_figheight()
771+
dpi = self.figure.get_dpi()
770772

771773
# create pgfpicture environment and write the pgf code
772774
fh.write(header_text)
@@ -777,7 +779,10 @@ def _print_pgf_to_fh(self, fh):
777779
writeln(fh, r"\begin{pgfpicture}")
778780
writeln(fh, r"\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{%fin}{%fin}}" % (w, h))
779781
writeln(fh, r"\pgfusepath{use as bounding box}")
780-
renderer = RendererPgf(self.figure, fh)
782+
_bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
783+
renderer = MixedModeRenderer(self.figure, w, h, dpi,
784+
RendererPgf(self.figure, fh),
785+
bbox_inches_restore=_bbox_inches_restore)
781786
self.figure.draw(renderer)
782787

783788
# end the pgfpicture environment
@@ -796,14 +801,14 @@ def print_pgf(self, fname_or_fh, *args, **kwargs):
796801
# figure out where the pgf is to be written to
797802
if is_string_like(fname_or_fh):
798803
with codecs.open(fname_or_fh, "w", encoding="utf-8") as fh:
799-
self._print_pgf_to_fh(fh)
804+
self._print_pgf_to_fh(fh, *args, **kwargs)
800805
elif is_writable_file_like(fname_or_fh):
801806
raise ValueError("saving pgf to a stream is not supported, " +
802807
"consider using the pdf option of the pgf-backend")
803808
else:
804809
raise ValueError("filename must be a path")
805810

806-
def _print_pdf_to_fh(self, fh):
811+
def _print_pdf_to_fh(self, fh, *args, **kwargs):
807812
w, h = self.figure.get_figwidth(), self.figure.get_figheight()
808813

809814
try:
@@ -814,7 +819,7 @@ def _print_pdf_to_fh(self, fh):
814819
fname_pdf = os.path.join(tmpdir, "figure.pdf")
815820

816821
# print figure to pgf and compile it with latex
817-
self.print_pgf(fname_pgf)
822+
self.print_pgf(fname_pgf, *args, **kwargs)
818823

819824
latex_preamble = get_preamble()
820825
latex_fontspec = get_fontspec()
@@ -856,13 +861,13 @@ def print_pdf(self, fname_or_fh, *args, **kwargs):
856861
# figure out where the pdf is to be written to
857862
if is_string_like(fname_or_fh):
858863
with open(fname_or_fh, "wb") as fh:
859-
self._print_pdf_to_fh(fh)
864+
self._print_pdf_to_fh(fh, *args, **kwargs)
860865
elif is_writable_file_like(fname_or_fh):
861-
self._print_pdf_to_fh(fname_or_fh)
866+
self._print_pdf_to_fh(fname_or_fh, *args, **kwargs)
862867
else:
863868
raise ValueError("filename must be a path or a file-like object")
864869

865-
def _print_png_to_fh(self, fh):
870+
def _print_png_to_fh(self, fh, *args, **kwargs):
866871
converter = make_pdf_to_png_converter()
867872

868873
try:
@@ -871,7 +876,7 @@ def _print_png_to_fh(self, fh):
871876
fname_pdf = os.path.join(tmpdir, "figure.pdf")
872877
fname_png = os.path.join(tmpdir, "figure.png")
873878
# create pdf and try to convert it to png
874-
self.print_pdf(fname_pdf)
879+
self.print_pdf(fname_pdf, *args, **kwargs)
875880
converter(fname_pdf, fname_png, dpi=self.figure.dpi)
876881
# copy file contents to target
877882
with open(fname_png, "rb") as fh_src:
@@ -888,9 +893,9 @@ def print_png(self, fname_or_fh, *args, **kwargs):
888893
"""
889894
if is_string_like(fname_or_fh):
890895
with open(fname_or_fh, "wb") as fh:
891-
self._print_png_to_fh(fh)
896+
self._print_png_to_fh(fh, *args, **kwargs)
892897
elif is_writable_file_like(fname_or_fh):
893-
self._print_png_to_fh(fname_or_fh)
898+
self._print_png_to_fh(fname_or_fh, *args, **kwargs)
894899
else:
895900
raise ValueError("filename must be a path or a file-like object")
896901

Binary file not shown.

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ def test_pathclip():
145145
plt.savefig(os.path.join(result_dir, "pgf_pathclip.pdf"))
146146

147147

148+
# test mixed mode rendering
149+
@switch_backend('pgf')
150+
def test_mixedmode():
151+
if not check_for('xelatex'):
152+
raise SkipTest('xelatex + pgf is required')
153+
154+
Y, X = np.ogrid[-1:1:40j, -1:1:40j]
155+
plt.figure()
156+
plt.pcolor(X**2 + Y**2).set_rasterized(True)
157+
compare_figure('pgf_mixedmode.pdf')
158+
159+
148160
if __name__ == '__main__':
149161
import nose
150162
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

0 commit comments

Comments
 (0)