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

Skip to content

Fix/zorder rasterization #24768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 68 additions & 11 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3127,23 +3127,23 @@ def draw(self, renderer):

if (rasterization_zorder is not None and
artists and artists[0].zorder < rasterization_zorder):
renderer.start_rasterizing()
artists_rasterized = [a for a in artists
if a.zorder < rasterization_zorder]
artists = [a for a in artists
if a.zorder >= rasterization_zorder]
split_index = np.searchsorted(
[art.zorder for art in artists],
rasterization_zorder, side='right'
)
artists_rasterized = artists[:split_index]
artists = artists[split_index:]
else:
artists_rasterized = []

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

if artists_rasterized:
for a in artists_rasterized:
a.draw(renderer)
renderer.stop_rasterizing()
_draw_rasterized(self.figure, artists_rasterized, renderer)

mimage._draw_list_compositing_images(
renderer, self, artists, self.figure.suppressComposite)
Expand Down Expand Up @@ -4636,3 +4636,60 @@ def _label_outer_yaxis(self, *, check_patch):
self.yaxis.set_tick_params(which="both", labelright=False)
if self.yaxis.offsetText.get_position()[0] == 1:
self.yaxis.offsetText.set_visible(False)


def _draw_rasterized(figure, artists, renderer):
"""
A helper function for rasterizing the list of artists.

The bookkeeping to track if we are or are not in rasterizing mode
with the mixed-mode backends is relatively complicated and is now
handled in the matplotlib.artist.allow_rasterization decorator.

This helper defines the absolute minimum methods and attributes on
shim class to be compatible with that decorator and the uses it to
rasterize the list of artists.

This is maybe too-clever, but allows us to re-use the same code that is
used on normal artists to participate in the "are we rasterizing"
accounting.

Please do not use this outside of the "rasterize below a given zorder"
functionality of Axes.

Parameters
----------
figure : matplotlib.figure.Figure
The figure all of the artists belong to (not checked). We need this
because we can at the figure level suppress composition and insert each
rasterized artist as it's own image.

artists : List[matplotlib.artist.Artist]
The list of Artists to be rasterized. These are assumed to all
be in the same Figure.

renderer : matplotlib.backendbases.RendererBase
The currently active renderer

Returns
-------
None

"""
class _MinimalArtist:
def get_rasterized(self):
return True

def get_agg_filter(self):
return None

def __init__(self, figure, artists):
self.figure = figure
self.artists = artists

@martist.allow_rasterization
def draw(self, renderer):
for a in self.artists:
a.draw(renderer)

return _MinimalArtist(figure, artists).draw(renderer)
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8449,3 +8449,11 @@ def get_next_color():
c = 'red\n'
mpl.axes.Axes._parse_scatter_color_args(
c, None, kwargs={}, xsize=2, get_next_color_func=get_next_color)


def test_zorder_and_explicit_rasterization():
fig, ax = plt.subplots()
ax.set_rasterization_zorder(5)
ln, = ax.plot(range(5), rasterized=True, zorder=1)
with io.BytesIO() as b:
fig.savefig(b, format='pdf')