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

Skip to content

Commit a34c2fd

Browse files
committed
per-artist rasterization
svn path=/trunk/matplotlib/; revision=7089
1 parent 0a1e4ee commit a34c2fd

12 files changed

Lines changed: 79 additions & 5 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
======================================================================
2+
2009-05-06 Per-artist Rasterization, originally by Eric Bruning. -JJ
3+
24
2009-05-05 Add an example that shows how to make a plot that updates
35
using data from another process. Thanks to Robert
46
Cimrman - RMM

lib/matplotlib/artist.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,38 @@
2222
# http://groups.google.com/groups?hl=en&lr=&threadm=mailman.5090.1098044946.5135.python-list%40python.org&rnum=1&prev=/groups%3Fq%3D__doc__%2Bauthor%253Ajdhunter%2540ace.bsd.uchicago.edu%26hl%3Den%26btnG%3DGoogle%2BSearch
2323

2424

25+
26+
27+
def allow_rasterization(draw):
28+
"""
29+
Decorator for Artist.draw method. Provides routines
30+
that run before and after the draw call. The before and after functions
31+
are useful for changing artist-dependant renderer attributes or making
32+
other setup function calls, such as starting and flushing a mixed-mode
33+
renderer.
34+
"""
35+
def before(artist, renderer):
36+
if artist.get_rasterized():
37+
renderer.start_rasterizing()
38+
39+
def after(artist, renderer):
40+
if artist.get_rasterized():
41+
renderer.stop_rasterizing()
42+
43+
# the axes class has a second argument inframe for its draw method.
44+
def draw_wrapper(artist, renderer, *kl):
45+
before(artist, renderer)
46+
draw(artist, renderer, *kl)
47+
after(artist, renderer)
48+
49+
# "safe wrapping" to exactly replicate anything we haven't overridden above
50+
draw_wrapper.__name__ = draw.__name__
51+
draw_wrapper.__dict__ = draw.__dict__
52+
draw_wrapper.__doc__ = draw.__doc__
53+
draw_wrapper._supports_rasterization = True
54+
return draw_wrapper
55+
56+
2557
class Artist(object):
2658
"""
2759
Abstract base class for someone who renders into a
@@ -45,6 +77,7 @@ def __init__(self):
4577
self._label = ''
4678
self._picker = None
4779
self._contains = None
80+
self._rasterized = None
4881

4982
self.eventson = False # fire events only if eventson
5083
self._oid = 0 # an observer id
@@ -510,6 +543,22 @@ def _set_gc_clip(self, gc):
510543
else:
511544
gc.set_clip_rectangle(None)
512545
gc.set_clip_path(None)
546+
547+
def get_rasterized(self):
548+
return self._rasterized
549+
550+
def set_rasterized(self, rasterized):
551+
"""
552+
Force rasterized (bitmap) drawing in vector backend output.
553+
554+
Defaults to None, which implies the backend's default behavior
555+
556+
ACCEPTS: [True | False | None]
557+
"""
558+
if rasterized and not hasattr(self.draw, "_supports_rasterization"):
559+
warnings.warn("Rasterization of '%s' will be ignored" % self)
560+
561+
self._rasterized = rasterized
513562

514563
def draw(self, renderer, *args, **kwargs):
515564
'Derived classes drawing method'

lib/matplotlib/axes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,7 @@ def autoscale_view(self, tight=False, scalex=True, scaley=True):
16021602

16031603
#### Drawing
16041604

1605+
@allow_rasterization
16051606
def draw(self, renderer=None, inframe=False):
16061607
"Draw everything (plot lines, axes, labels)"
16071608
if renderer is None:

lib/matplotlib/axis.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from matplotlib import rcParams
77
import matplotlib.artist as artist
8+
from matplotlib.artist import allow_rasterization
89
import matplotlib.cbook as cbook
910
import matplotlib.font_manager as font_manager
1011
import matplotlib.lines as mlines
@@ -176,6 +177,7 @@ def get_loc(self):
176177
'Return the tick location (data coords) as a scalar'
177178
return self._loc
178179

180+
@allow_rasterization
179181
def draw(self, renderer):
180182
if not self.get_visible(): return
181183
renderer.open_group(self.__name__)
@@ -719,6 +721,7 @@ def get_ticklabel_extents(self, renderer):
719721
bbox2 = mtransforms.Bbox.from_extents(0, 0, 0, 0)
720722
return bbox, bbox2
721723

724+
@allow_rasterization
722725
def draw(self, renderer, *args, **kwargs):
723726
'Draw the axis lines, grid lines, tick lines and labels'
724727
ticklabelBoxes = []

lib/matplotlib/collections.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import matplotlib.cm as cm
1818
import matplotlib.transforms as transforms
1919
import matplotlib.artist as artist
20+
from matplotlib.artist import allow_rasterization
2021
import matplotlib.backend_bases as backend_bases
2122
import matplotlib.path as mpath
2223
import matplotlib.mlab as mlab
@@ -190,6 +191,7 @@ def _prepare_points(self):
190191

191192
return transform, transOffset, offsets, paths
192193

194+
@allow_rasterization
193195
def draw(self, renderer):
194196
if not self.get_visible(): return
195197
renderer.open_group(self.__class__.__name__)
@@ -594,6 +596,7 @@ def convert_mesh_to_paths(meshWidth, meshHeight, coordinates):
594596
def get_datalim(self, transData):
595597
return self._bbox
596598

599+
@allow_rasterization
597600
def draw(self, renderer):
598601
if not self.get_visible(): return
599602
renderer.open_group(self.__class__.__name__)
@@ -781,6 +784,7 @@ def __init__(self,
781784

782785
__init__.__doc__ = cbook.dedent(__init__.__doc__) % artist.kwdocd
783786

787+
@allow_rasterization
784788
def draw(self, renderer):
785789
self._transforms = [
786790
transforms.Affine2D().rotate(-self._rotation).scale(

lib/matplotlib/figure.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import time
1616

1717
import artist
18-
from artist import Artist
18+
from artist import Artist, allow_rasterization
1919
from axes import Axes, SubplotBase, subplot_class_factory
2020
from cbook import flatten, allequal, Stack, iterable, dedent
2121
import _image
@@ -727,6 +727,7 @@ def clear(self):
727727
"""
728728
self.clf()
729729

730+
@allow_rasterization
730731
def draw(self, renderer):
731732
"""
732733
Render the figure using :class:`matplotlib.backend_bases.RendererBase` instance renderer

lib/matplotlib/image.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from matplotlib import rcParams
1313
import matplotlib.artist as martist
14+
from matplotlib.artist import allow_rasterization
1415
import matplotlib.colors as mcolors
1516
import matplotlib.cm as cm
1617
import matplotlib.cbook as cbook
@@ -225,7 +226,7 @@ def make_image(self, magnification=1.0):
225226
norm=self._filternorm, radius=self._filterrad)
226227
return im
227228

228-
229+
@allow_rasterization
229230
def draw(self, renderer, *args, **kwargs):
230231
if not self.get_visible(): return
231232
if (self.axes.get_xscale() != 'linear' or
@@ -571,6 +572,7 @@ def make_image(self, magnification=1.0):
571572
im.is_grayscale = self.is_grayscale
572573
return im
573574

575+
@allow_rasterization
574576
def draw(self, renderer, *args, **kwargs):
575577
if not self.get_visible(): return
576578
im = self.make_image(renderer.get_image_magnification())
@@ -723,6 +725,7 @@ def make_image(self, magnification=1.0):
723725

724726
return im
725727

728+
@allow_rasterization
726729
def draw(self, renderer, *args, **kwargs):
727730
if not self.get_visible(): return
728731
# todo: we should be able to do some cacheing here

lib/matplotlib/legend.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import numpy as np
2727

2828
from matplotlib import rcParams
29-
from matplotlib.artist import Artist
29+
from matplotlib.artist import Artist, allow_rasterization
3030
from matplotlib.cbook import is_string_like, iterable, silent_list, safezip
3131
from matplotlib.font_manager import FontProperties
3232
from matplotlib.lines import Line2D
@@ -323,6 +323,7 @@ def _findoffset_loc(self, width, height, xdescent, ydescent, renderer):
323323

324324
return x+xdescent, y+ydescent
325325

326+
@allow_rasterization
326327
def draw(self, renderer):
327328
"Draw everything that belongs to the legend"
328329
if not self.get_visible(): return

lib/matplotlib/lines.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from transforms import Affine2D, Bbox, TransformedPath, IdentityTransform
1919

2020
from matplotlib import rcParams
21+
from artist import allow_rasterization
22+
2123
# special-purpose marker identifiers:
2224
(TICKLEFT, TICKRIGHT, TICKUP, TICKDOWN,
2325
CARETLEFT, CARETRIGHT, CARETUP, CARETDOWN) = range(8)
@@ -459,6 +461,7 @@ def _is_sorted(self, x):
459461
if len(x)<2: return 1
460462
return np.alltrue(x[1:]-x[0:-1]>=0)
461463

464+
@allow_rasterization
462465
def draw(self, renderer):
463466
if self._invalid:
464467
self.recache()

lib/matplotlib/patches.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import matplotlib.cbook as cbook
99
import matplotlib.artist as artist
10+
from matplotlib.artist import allow_rasterization
1011
import matplotlib.colors as colors
1112
import matplotlib.transforms as transforms
1213
from matplotlib.path import Path
@@ -260,7 +261,7 @@ def get_hatch(self):
260261
'Return the current hatching pattern'
261262
return self._hatch
262263

263-
264+
@allow_rasterization
264265
def draw(self, renderer):
265266
'Draw the :class:`Patch` to the given *renderer*.'
266267
if not self.get_visible(): return
@@ -1176,6 +1177,7 @@ def __init__(self, xy, width, height, angle=0.0, theta1=0.0, theta2=360.0, **kwa
11761177
self.theta2 = theta2
11771178
__init__.__doc__ = cbook.dedent(__init__.__doc__) % artist.kwdocd
11781179

1180+
@allow_rasterization
11791181
def draw(self, renderer):
11801182
"""
11811183
Ellipses are normally drawn using an approximation that uses

0 commit comments

Comments
 (0)