Skip to content

Commit e83ff55

Browse files
committed
Artist's draw method by default prevents rasterization
1 parent 6c721e9 commit e83ff55

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

lib/matplotlib/artist.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@
2020

2121
_log = logging.getLogger(__name__)
2222

23+
def _prevent_rasterization(draw):
24+
# we assume that by default artists are not allowed to rasterize (unless
25+
# its draw method is explicitly decorated). If it is being drawn after a
26+
# rasterized artist and it has reached the rater_depth of 0. We stop
27+
# rasterization so that it does not affect the behavior of normal artist
28+
# (e.g., change in dpi). If the artist's draw method is decorated
29+
# (draw._supports_rasterization is True), it won't be decorated by
30+
# `_prevent_rasterization`.
31+
32+
if hasattr(draw, "_supports_rasterization"):
33+
return draw
34+
35+
@wraps(draw)
36+
def draw_wrapper(artist, renderer):
37+
if renderer._raster_depth == 0 and renderer._rasterizing:
38+
# Only stop when we are not in a rasterized parent
39+
# and something has be rasterized since last stop
40+
renderer.stop_rasterizing()
41+
renderer._rasterizing = False
42+
43+
return draw(artist, renderer)
44+
45+
return draw_wrapper
46+
2347

2448
def allow_rasterization(draw):
2549
"""
@@ -119,6 +143,8 @@ def __init_subclass__(cls):
119143
cls.set.__qualname__ = f"{cls.__qualname__}.set"
120144
cls._update_set_signature_and_docstring()
121145

146+
cls.draw = _prevent_rasterization(cls.draw)
147+
122148
_PROPERTIES_EXCLUDED_FROM_SET = [
123149
'navigate_mode', # not a user-facing function
124150
'figure', # changing the figure is such a profound operation
@@ -921,7 +947,10 @@ def set_rasterized(self, rasterized):
921947
----------
922948
rasterized : bool
923949
"""
924-
if rasterized and not hasattr(self.draw, "_supports_rasterization"):
950+
support_rasterization = getattr(self.draw,
951+
"_supports_rasterization", False)
952+
if (rasterized and
953+
not getattr(self.draw, "_supports_rasterization", False)):
925954
_api.warn_external(f"Rasterization of '{self}' will be ignored")
926955

927956
self._rasterized = rasterized

0 commit comments

Comments
 (0)