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

Skip to content

Commit f28b843

Browse files
authored
Merge pull request #24768 from tacaswell/fix/zorder_rasterization
Fix/zorder rasterization
2 parents cd8420a + 2372c3b commit f28b843

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,23 +3125,23 @@ def draw(self, renderer):
31253125

31263126
if (rasterization_zorder is not None and
31273127
artists and artists[0].zorder < rasterization_zorder):
3128-
renderer.start_rasterizing()
3129-
artists_rasterized = [a for a in artists
3130-
if a.zorder < rasterization_zorder]
3131-
artists = [a for a in artists
3132-
if a.zorder >= rasterization_zorder]
3128+
split_index = np.searchsorted(
3129+
[art.zorder for art in artists],
3130+
rasterization_zorder, side='right'
3131+
)
3132+
artists_rasterized = artists[:split_index]
3133+
artists = artists[split_index:]
31333134
else:
31343135
artists_rasterized = []
31353136

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

31413143
if artists_rasterized:
3142-
for a in artists_rasterized:
3143-
a.draw(renderer)
3144-
renderer.stop_rasterizing()
3144+
_draw_rasterized(self.figure, artists_rasterized, renderer)
31453145

31463146
mimage._draw_list_compositing_images(
31473147
renderer, self, artists, self.figure.suppressComposite)
@@ -4634,3 +4634,60 @@ def _label_outer_yaxis(self, *, check_patch):
46344634
self.yaxis.set_tick_params(which="both", labelright=False)
46354635
if self.yaxis.offsetText.get_position()[0] == 1:
46364636
self.yaxis.offsetText.set_visible(False)
4637+
4638+
4639+
def _draw_rasterized(figure, artists, renderer):
4640+
"""
4641+
A helper function for rasterizing the list of artists.
4642+
4643+
The bookkeeping to track if we are or are not in rasterizing mode
4644+
with the mixed-mode backends is relatively complicated and is now
4645+
handled in the matplotlib.artist.allow_rasterization decorator.
4646+
4647+
This helper defines the absolute minimum methods and attributes on a
4648+
shim class to be compatible with that decorator and then uses it to
4649+
rasterize the list of artists.
4650+
4651+
This is maybe too-clever, but allows us to re-use the same code that is
4652+
used on normal artists to participate in the "are we rasterizing"
4653+
accounting.
4654+
4655+
Please do not use this outside of the "rasterize below a given zorder"
4656+
functionality of Axes.
4657+
4658+
Parameters
4659+
----------
4660+
figure : matplotlib.figure.Figure
4661+
The figure all of the artists belong to (not checked). We need this
4662+
because we can at the figure level suppress composition and insert each
4663+
rasterized artist as its own image.
4664+
4665+
artists : List[matplotlib.artist.Artist]
4666+
The list of Artists to be rasterized. These are assumed to all
4667+
be in the same Figure.
4668+
4669+
renderer : matplotlib.backendbases.RendererBase
4670+
The currently active renderer
4671+
4672+
Returns
4673+
-------
4674+
None
4675+
4676+
"""
4677+
class _MinimalArtist:
4678+
def get_rasterized(self):
4679+
return True
4680+
4681+
def get_agg_filter(self):
4682+
return None
4683+
4684+
def __init__(self, figure, artists):
4685+
self.figure = figure
4686+
self.artists = artists
4687+
4688+
@martist.allow_rasterization
4689+
def draw(self, renderer):
4690+
for a in self.artists:
4691+
a.draw(renderer)
4692+
4693+
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+
with io.BytesIO() as b:
8459+
fig.savefig(b, format='pdf')

0 commit comments

Comments
 (0)