Skip to content

Commit c27a1af

Browse files
committed
FIX: correctly manage rasterization state via zoreder
If an artist was rasterized both by setting state on the artist and be being below the z-order threshold than we would try to stop rasterization one too many times. This constructions lets us re-use the `allow_rasterization` logic. closes matplotlib#24235
1 parent ecf6e26 commit c27a1af

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,23 +3127,21 @@ def draw(self, renderer):
31273127

31283128
if (rasterization_zorder is not None and
31293129
artists and artists[0].zorder < rasterization_zorder):
3130-
renderer.start_rasterizing()
31313130
artists_rasterized = [a for a in artists
31323131
if a.zorder < rasterization_zorder]
31333132
artists = [a for a in artists
31343133
if a.zorder >= rasterization_zorder]
31353134
else:
31363135
artists_rasterized = []
31373136

3138-
# the patch draws the background rectangle -- the frame below
3139-
# will draw the edges
31403137
if self.axison and self._frameon:
3141-
self.patch.draw(renderer)
3138+
if artists_rasterized:
3139+
artists_rasterized = [self.patch] + artists_rasterized
3140+
else:
3141+
artists = [self.patch] + artists
31423142

31433143
if artists_rasterized:
3144-
for a in artists_rasterized:
3145-
a.draw(renderer)
3146-
renderer.stop_rasterizing()
3144+
_draw_rasterized(self.figure, artists_rasterized, renderer)
31473145

31483146
mimage._draw_list_compositing_images(
31493147
renderer, self, artists, self.figure.suppressComposite)
@@ -4636,3 +4634,23 @@ def _label_outer_yaxis(self, *, check_patch):
46364634
self.yaxis.set_tick_params(which="both", labelright=False)
46374635
if self.yaxis.offsetText.get_position()[0] == 1:
46384636
self.yaxis.offsetText.set_visible(False)
4637+
4638+
4639+
def _draw_rasterized(figure, artists, renderer):
4640+
class _MinimalArtist:
4641+
def get_rasterized(self):
4642+
return True
4643+
4644+
def get_agg_filter(self):
4645+
return None
4646+
4647+
def __init__(self, figure, artists):
4648+
self.figure = figure
4649+
self.artists = artists
4650+
4651+
@martist.allow_rasterization
4652+
def draw(self, renderer):
4653+
for a in self.artists:
4654+
a.draw(renderer)
4655+
4656+
return _MinimalArtist(figure, artists).draw(renderer)

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8449,3 +8449,11 @@ def get_next_color():
84498449
c = 'red\n'
84508450
mpl.axes.Axes._parse_scatter_color_args(
84518451
c, None, kwargs={}, xsize=2, get_next_color_func=get_next_color)
8452+
8453+
8454+
def test_zorder_and_explicit_rasterization():
8455+
fig, ax = plt.subplots()
8456+
ax.set_rasterization_zorder(5)
8457+
ln, = ax.plot(range(5), rasterized=True, zorder=1)
8458+
b = io.BytesIO()
8459+
fig.savefig(b, format='pdf')

0 commit comments

Comments
 (0)