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

Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
pgf: add mixed mode rendering
  • Loading branch information
pwuertz committed May 25, 2013
commit b25e5067b03681719bdbc553574a3991c935857c
27 changes: 16 additions & 11 deletions lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff 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 Down Expand 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 Down Expand 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 Down Expand 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
Binary file not shown.
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_backend_pgf.py
Original file line number Diff line number Diff 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)