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

MixedModeRenderer non-72-dpi fixes & Pgf mixed rendering#1975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
pwuertz merged 2 commits intomatplotlib:masterfrompwuertz:pgf_mixedrenderer
May 27, 2013
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletionslib/matplotlib/backends/backend_mixed.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,6 +48,7 @@ def __init__(self, figure, width, height, dpi, vector_renderer,
# the figure dpi before and after the rasterization. Although
# this looks ugly, I couldn't find a better solution. -JJL
self.figure=figure
self._figdpi = figure.get_dpi()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Just spotted this one too.


self._bbox_inches_restore = bbox_inches_restore

Expand DownExpand Up@@ -121,16 +122,19 @@ def stop_rasterizing(self):
image.is_grayscale = False
image.flipud_out()
gc = self._renderer.new_gc()
# TODO: If the mixedmode resolution differs from the figure's
# dpi, the image must be scaled (dpi->_figdpi). Not all
# backends support this.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

And this too. (#1185 (comment))

self._renderer.draw_image(
gc,
float(l)/self.dpi*72.,
(float(height) - b - h)/self.dpi*72.,
float(l) /self.dpi * self._figdpi,
(float(height)-b-h) /self.dpi * self._figdpi,
image)
self._raster_renderer = None
self._rasterizing = False

# restore the figure dpi.
self.figure.set_dpi(72)
# restore the figure dpi.
self.figure.set_dpi(self._figdpi)

if self._bbox_inches_restore: # when tight bbox is used
r = process_figure_for_rasterizing(self.figure,
Expand Down
27 changes: 16 additions & 11 deletionslib/matplotlib/backends/backend_pgf.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@
import matplotlib as mpl
from matplotlib.backend_bases import RendererBase, GraphicsContextBase,\
FigureManagerBase, FigureCanvasBase
from matplotlib.backends.backend_mixed import MixedModeRenderer
from matplotlib.figure import Figure
from matplotlib.text import Text
from matplotlib.path import Path
Expand DownExpand Up@@ -738,7 +739,7 @@ def __init__(self, *args):
def get_default_filetype(self):
return 'pdf'

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

# get figure size in inch
w, h = self.figure.get_figwidth(), self.figure.get_figheight()
dpi = self.figure.get_dpi()

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

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

def _print_pdf_to_fh(self, fh):
def _print_pdf_to_fh(self, fh, *args, **kwargs):
w, h = self.figure.get_figwidth(), self.figure.get_figheight()

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

# print figure to pgf and compile it with latex
self.print_pgf(fname_pgf)
self.print_pgf(fname_pgf, *args, **kwargs)

latex_preamble = get_preamble()
latex_fontspec = get_fontspec()
Expand DownExpand Up@@ -856,13 +861,13 @@ def print_pdf(self, fname_or_fh, *args, **kwargs):
# figure out where the pdf is to be written to
if is_string_like(fname_or_fh):
with open(fname_or_fh, "wb") as fh:
self._print_pdf_to_fh(fh)
self._print_pdf_to_fh(fh, *args, **kwargs)
elif is_writable_file_like(fname_or_fh):
self._print_pdf_to_fh(fname_or_fh)
self._print_pdf_to_fh(fname_or_fh, *args, **kwargs)
else:
raise ValueError("filename must be a path or a file-like object")

def _print_png_to_fh(self, fh):
def _print_png_to_fh(self, fh, *args, **kwargs):
converter = make_pdf_to_png_converter()

try:
Expand All@@ -871,7 +876,7 @@ def _print_png_to_fh(self, fh):
fname_pdf = os.path.join(tmpdir, "figure.pdf")
fname_png = os.path.join(tmpdir, "figure.png")
# create pdf and try to convert it to png
self.print_pdf(fname_pdf)
self.print_pdf(fname_pdf, *args, **kwargs)
converter(fname_pdf, fname_png, dpi=self.figure.dpi)
# copy file contents to target
with open(fname_png, "rb") as fh_src:
Expand All@@ -888,9 +893,9 @@ def print_png(self, fname_or_fh, *args, **kwargs):
"""
if is_string_like(fname_or_fh):
with open(fname_or_fh, "wb") as fh:
self._print_png_to_fh(fh)
self._print_png_to_fh(fh, *args, **kwargs)
elif is_writable_file_like(fname_or_fh):
self._print_png_to_fh(fname_or_fh)
self._print_png_to_fh(fname_or_fh, *args, **kwargs)
else:
raise ValueError("filename must be a path or a file-like object")

Expand Down
View file
Open in desktop
Binary file not shown.
12 changes: 12 additions & 0 deletionslib/matplotlib/tests/test_backend_pgf.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -145,6 +145,18 @@ def test_pathclip():
plt.savefig(os.path.join(result_dir, "pgf_pathclip.pdf"))


# test mixed mode rendering
@switch_backend('pgf')
def test_mixedmode():
if not check_for('xelatex'):
raise SkipTest('xelatex + pgf is required')

Y, X = np.ogrid[-1:1:40j, -1:1:40j]
plt.figure()
plt.pcolor(X**2 + Y**2).set_rasterized(True)
compare_figure('pgf_mixedmode.pdf')


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

[8]ページ先頭

©2009-2025 Movatter.jp