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

Skip to content

Commit 83b6a42

Browse files
committed
print_ps with mixed-renderer
svn path=/trunk/matplotlib/; revision=7090
1 parent a34c2fd commit 83b6a42

4 files changed

Lines changed: 137 additions & 47 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
======================================================================
2+
2009-05-06 print_ps now uses mixed-mode renderer. Axes.draw rasterize
3+
artists whose zorder smaller than rasterization_zorder.
4+
-JJL
5+
26
2009-05-06 Per-artist Rasterization, originally by Eric Bruning. -JJ
37

48
2009-05-05 Add an example that shows how to make a plot that updates

lib/matplotlib/axes.py

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
rcParams = matplotlib.rcParams
99

1010
import matplotlib.artist as martist
11+
from matplotlib.artist import allow_rasterization
1112
import matplotlib.axis as maxis
1213
import matplotlib.cbook as cbook
1314
import matplotlib.collections as mcoll
@@ -531,6 +532,8 @@ def __init__(self, fig, rect,
531532
self._frameon = frameon
532533
self._axisbelow = rcParams['axes.axisbelow']
533534

535+
self._rasterization_zorder = -30000
536+
534537
self._hold = rcParams['axes.hold']
535538
self._connected = {} # a dict from events to (id, func)
536539
self.cla()
@@ -1566,6 +1569,19 @@ def set_autoscaley_on(self, b):
15661569
"""
15671570
self._autoscaleYon = b
15681571

1572+
def set_rasterization_zorder(self, z):
1573+
"""
1574+
Set zorder value below which artists will be rasterized
1575+
"""
1576+
self._rasterization_zorder = z
1577+
1578+
def get_rasterization_zorder(self):
1579+
"""
1580+
Get zorder value below which artists will be rasterized
1581+
"""
1582+
return self._rasterization_zorder
1583+
1584+
15691585
def autoscale_view(self, tight=False, scalex=True, scaley=True):
15701586
"""
15711587
autoscale the view limits using the data limits. You can
@@ -1620,14 +1636,54 @@ def draw(self, renderer=None, inframe=False):
16201636
else:
16211637
self.apply_aspect()
16221638

1623-
# the patch draws the background rectangle -- the frame below
1624-
# will draw the edges
1625-
if self.axison and self._frameon:
1626-
self.patch.draw(renderer)
16271639

16281640
artists = []
16291641

1642+
artists.extend(self.collections)
1643+
artists.extend(self.patches)
1644+
artists.extend(self.lines)
1645+
artists.extend(self.texts)
1646+
artists.extend(self.artists)
1647+
if self.axison and not inframe:
1648+
if self._axisbelow:
1649+
self.xaxis.set_zorder(0.5)
1650+
self.yaxis.set_zorder(0.5)
1651+
else:
1652+
self.xaxis.set_zorder(2.5)
1653+
self.yaxis.set_zorder(2.5)
1654+
artists.extend([self.xaxis, self.yaxis])
1655+
if not inframe: artists.append(self.title)
1656+
artists.extend(self.tables)
1657+
if self.legend_ is not None:
1658+
artists.append(self.legend_)
1659+
1660+
# the frame draws the edges around the axes patch -- we
1661+
# decouple these so the patch can be in the background and the
1662+
# frame in the foreground.
1663+
if self.axison and self._frameon:
1664+
artists.append(self.frame)
1665+
16301666

1667+
dsu = [ (a.zorder, i, a) for i, a in enumerate(artists)
1668+
if not a.get_animated() ]
1669+
dsu.sort()
1670+
1671+
1672+
# rasterze artists with negative zorder
1673+
# if the minimum zorder is negative, start rasterization
1674+
rasterization_zorder = self._rasterization_zorder
1675+
if len(dsu) > 0 and dsu[0][0] < rasterization_zorder:
1676+
renderer.start_rasterizing()
1677+
dsu_rasterized = [l for l in dsu if l[0] < rasterization_zorder]
1678+
dsu = [l for l in dsu if l[0] >= rasterization_zorder]
1679+
else:
1680+
dsu_rasterized = []
1681+
1682+
1683+
# the patch draws the background rectangle -- the frame below
1684+
# will draw the edges
1685+
if self.axison and self._frameon:
1686+
self.patch.draw(renderer)
16311687

16321688
if len(self.images)<=1 or renderer.option_image_nocomposite():
16331689
for im in self.images:
@@ -1657,34 +1713,12 @@ def draw(self, renderer=None, inframe=False):
16571713
self.patch.get_path(),
16581714
self.patch.get_transform())
16591715

1660-
artists.extend(self.collections)
1661-
artists.extend(self.patches)
1662-
artists.extend(self.lines)
1663-
artists.extend(self.texts)
1664-
artists.extend(self.artists)
1665-
if self.axison and not inframe:
1666-
if self._axisbelow:
1667-
self.xaxis.set_zorder(0.5)
1668-
self.yaxis.set_zorder(0.5)
1669-
else:
1670-
self.xaxis.set_zorder(2.5)
1671-
self.yaxis.set_zorder(2.5)
1672-
artists.extend([self.xaxis, self.yaxis])
1673-
if not inframe: artists.append(self.title)
1674-
artists.extend(self.tables)
1675-
if self.legend_ is not None:
1676-
artists.append(self.legend_)
1677-
1678-
# the frame draws the edges around the axes patch -- we
1679-
# decouple these so the patch can be in the background and the
1680-
# frame in the foreground.
1681-
if self.axison and self._frameon:
1682-
artists.append(self.frame)
16831716

16841717

1685-
dsu = [ (a.zorder, i, a) for i, a in enumerate(artists)
1686-
if not a.get_animated() ]
1687-
dsu.sort()
1718+
if dsu_rasterized:
1719+
for zorder, i, a in dsu_rasterized:
1720+
a.draw(renderer)
1721+
renderer.stop_rasterizing()
16881722

16891723
for zorder, i, a in dsu:
16901724
a.draw(renderer)

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,7 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
14431443
facecolor=facecolor,
14441444
edgecolor=edgecolor,
14451445
orientation=orientation,
1446+
dryrun=True,
14461447
**kwargs)
14471448
renderer = self.figure._cachedRenderer
14481449
bbox_inches = self.figure.get_tightbbox(renderer)

lib/matplotlib/backends/backend_ps.py

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
3333
from matplotlib.path import Path
3434
from matplotlib.transforms import Affine2D
3535

36+
from matplotlib.backends.backend_mixed import MixedModeRenderer
37+
38+
3639
import numpy as npy
3740
import binascii
3841
import re
@@ -843,34 +846,42 @@ def print_ps(self, outfile, *args, **kwargs):
843846
def print_eps(self, outfile, *args, **kwargs):
844847
return self._print_ps(outfile, 'eps', *args, **kwargs)
845848

849+
850+
851+
852+
853+
846854
def _print_ps(self, outfile, format, *args, **kwargs):
847-
papertype = kwargs.get("papertype", rcParams['ps.papersize'])
855+
papertype = kwargs.pop("papertype", rcParams['ps.papersize'])
848856
papertype = papertype.lower()
849857
if papertype == 'auto':
850858
pass
851859
elif papertype not in papersize:
852860
raise RuntimeError( '%s is not a valid papertype. Use one \
853861
of %s'% (papertype, ', '.join( papersize.keys() )) )
854862

855-
orientation = kwargs.get("orientation", "portrait").lower()
863+
orientation = kwargs.pop("orientation", "portrait").lower()
856864
if orientation == 'landscape': isLandscape = True
857865
elif orientation == 'portrait': isLandscape = False
858866
else: raise RuntimeError('Orientation must be "portrait" or "landscape"')
859867

860868
self.figure.set_dpi(72) # Override the dpi kwarg
861-
imagedpi = kwargs.get("dpi", 72)
862-
facecolor = kwargs.get("facecolor", "w")
863-
edgecolor = kwargs.get("edgecolor", "w")
869+
imagedpi = kwargs.pop("dpi", 72)
870+
facecolor = kwargs.pop("facecolor", "w")
871+
edgecolor = kwargs.pop("edgecolor", "w")
864872

865873
if rcParams['text.usetex']:
866874
self._print_figure_tex(outfile, format, imagedpi, facecolor, edgecolor,
867-
orientation, isLandscape, papertype)
875+
orientation, isLandscape, papertype,
876+
**kwargs)
868877
else:
869878
self._print_figure(outfile, format, imagedpi, facecolor, edgecolor,
870-
orientation, isLandscape, papertype)
879+
orientation, isLandscape, papertype,
880+
**kwargs)
871881

872882
def _print_figure(self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
873-
orientation='portrait', isLandscape=False, papertype=None):
883+
orientation='portrait', isLandscape=False, papertype=None,
884+
**kwargs):
874885
"""
875886
Render the figure to hardcopy. Set the figure patch face and
876887
edge colors. This is useful because some of the GUIs have a
@@ -939,10 +950,30 @@ def _print_figure(self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
939950
self.figure.set_facecolor(facecolor)
940951
self.figure.set_edgecolor(edgecolor)
941952

942-
self._pswriter = StringIO()
943-
renderer = RendererPS(width, height, self._pswriter, imagedpi=dpi)
953+
954+
dryrun = kwargs.get("dryrun", False)
955+
if dryrun:
956+
class NullWriter(object):
957+
def write(self, *kl, **kwargs):
958+
pass
959+
960+
self._pswriter = NullWriter()
961+
else:
962+
self._pswriter = StringIO()
963+
964+
965+
# mixed mode rendering
966+
_bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
967+
ps_renderer = RendererPS(width, height, self._pswriter, imagedpi=dpi)
968+
renderer = MixedModeRenderer(self.figure,
969+
width, height, dpi, ps_renderer,
970+
bbox_inches_restore=_bbox_inches_restore)
971+
944972
self.figure.draw(renderer)
945973

974+
if dryrun: # return immediately if dryrun (tightbbox=True)
975+
return
976+
946977
self.figure.set_facecolor(origfacecolor)
947978
self.figure.set_edgecolor(origedgecolor)
948979

@@ -962,15 +993,15 @@ def _print_figure(self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
962993
Ndict = len(psDefs)
963994
print >>fh, "%%BeginProlog"
964995
if not rcParams['ps.useafm']:
965-
Ndict += len(renderer.used_characters)
996+
Ndict += len(ps_renderer.used_characters)
966997
print >>fh, "/mpldict %d dict def"%Ndict
967998
print >>fh, "mpldict begin"
968999
for d in psDefs:
9691000
d=d.strip()
9701001
for l in d.split('\n'):
9711002
print >>fh, l.strip()
9721003
if not rcParams['ps.useafm']:
973-
for font_filename, chars in renderer.used_characters.values():
1004+
for font_filename, chars in ps_renderer.used_characters.values():
9741005
if len(chars):
9751006
font = FT2Font(font_filename)
9761007
cmap = font.get_charmap()
@@ -1019,7 +1050,8 @@ def _print_figure(self, outfile, format, dpi=72, facecolor='w', edgecolor='w',
10191050
shutil.move(tmpfile, outfile)
10201051

10211052
def _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor,
1022-
orientation, isLandscape, papertype):
1053+
orientation, isLandscape, papertype,
1054+
**kwargs):
10231055
"""
10241056
If text.usetex is True in rc, a temporary pair of tex/eps files
10251057
are created to allow tex to manage the text layout via the PSFrags
@@ -1051,10 +1083,29 @@ def _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor,
10511083
self.figure.set_facecolor(facecolor)
10521084
self.figure.set_edgecolor(edgecolor)
10531085

1054-
self._pswriter = StringIO()
1055-
renderer = RendererPS(width, height, self._pswriter, imagedpi=dpi)
1086+
dryrun = kwargs.get("dryrun", False)
1087+
if dryrun:
1088+
class NullWriter(object):
1089+
def write(self, *kl, **kwargs):
1090+
pass
1091+
1092+
self._pswriter = NullWriter()
1093+
else:
1094+
self._pswriter = StringIO()
1095+
1096+
1097+
# mixed mode rendering
1098+
_bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
1099+
ps_renderer = RendererPS(width, height, self._pswriter, imagedpi=dpi)
1100+
renderer = MixedModeRenderer(self.figure,
1101+
width, height, dpi, ps_renderer,
1102+
bbox_inches_restore=_bbox_inches_restore)
1103+
10561104
self.figure.draw(renderer)
10571105

1106+
if dryrun: # return immediately if dryrun (tightbbox=True)
1107+
return
1108+
10581109
self.figure.set_facecolor(origfacecolor)
10591110
self.figure.set_edgecolor(origedgecolor)
10601111

@@ -1117,11 +1168,11 @@ def _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor,
11171168
paper will be used to prevent clipping.'%(papertype, temp_papertype), 'helpful')
11181169

11191170

1120-
texmanager = renderer.get_texmanager()
1171+
texmanager = ps_renderer.get_texmanager()
11211172
font_preamble = texmanager.get_font_preamble()
11221173
custom_preamble = texmanager.get_custom_preamble()
11231174

1124-
convert_psfrags(tmpfile, renderer.psfrag, font_preamble,
1175+
convert_psfrags(tmpfile, ps_renderer.psfrag, font_preamble,
11251176
custom_preamble, paperWidth, paperHeight,
11261177
orientation)
11271178

0 commit comments

Comments
 (0)