Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitca293ce

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

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
ifself._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
importmatplotlibasmpl
1414
frommatplotlib.backend_basesimportRendererBase,GraphicsContextBase,\
1515
FigureManagerBase,FigureCanvasBase
16+
frommatplotlib.backends.backend_mixedimportMixedModeRenderer
1617
frommatplotlib.figureimportFigure
1718
frommatplotlib.textimportText
1819
frommatplotlib.pathimportPath
@@ -738,7 +739,7 @@ def __init__(self, *args):
738739
defget_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
ifis_string_like(fname_or_fh):
798803
withcodecs.open(fname_or_fh,"w",encoding="utf-8")asfh:
799-
self._print_pgf_to_fh(fh)
804+
self._print_pgf_to_fh(fh,*args,**kwargs)
800805
elifis_writable_file_like(fname_or_fh):
801806
raiseValueError("saving pgf to a stream is not supported, "+
802807
"consider using the pdf option of the pgf-backend")
803808
else:
804809
raiseValueError("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
ifis_string_like(fname_or_fh):
858863
withopen(fname_or_fh,"wb")asfh:
859-
self._print_pdf_to_fh(fh)
864+
self._print_pdf_to_fh(fh,*args,**kwargs)
860865
elifis_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
raiseValueError("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
withopen(fname_png,"rb")asfh_src:
@@ -888,9 +893,9 @@ def print_png(self, fname_or_fh, *args, **kwargs):
888893
"""
889894
ifis_string_like(fname_or_fh):
890895
withopen(fname_or_fh,"wb")asfh:
891-
self._print_png_to_fh(fh)
896+
self._print_png_to_fh(fh,*args,**kwargs)
892897
elifis_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
raiseValueError("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+
deftest_mixedmode():
151+
ifnotcheck_for('xelatex'):
152+
raiseSkipTest('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
importnose
150162
nose.runmodule(argv=['-s','--with-doctest'],exit=False)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp