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

Skip to content

[Bug]: pcolormesh(rasterized=True) conflicts with set_rasterization_zorder() #24235

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

Closed
EdwinChan opened this issue Oct 20, 2022 · 1 comment · Fixed by #24768
Closed

[Bug]: pcolormesh(rasterized=True) conflicts with set_rasterization_zorder() #24235

EdwinChan opened this issue Oct 20, 2022 · 1 comment · Fixed by #24768

Comments

@EdwinChan
Copy link

EdwinChan commented Oct 20, 2022

Bug summary

According to the documentation, a color plot can be rasterized in two ways:

  • pyplot.pcolormesh(…, rasterized=True)
  • pyplot.gca().set_rasterization_zorder(…)

The two ways cannot be used together.

Code for reproduction

import math
import numpy
import numpy.random
import matplotlib
from matplotlib import pyplot

matplotlib.use('agg')

r = numpy.linspace(1, 10, 10+1)
p = numpy.linspace(-math.pi, math.pi, 36+1)
r, p = numpy.meshgrid(r, p)
x, y = r*numpy.cos(p), r*numpy.sin(p)
s = tuple(s-1 for s in x.shape)
z = numpy.random.default_rng(0).uniform(size=s)

pyplot.pcolormesh(x, y, z, rasterized=True, zorder=-11)
pyplot.gca().set_rasterization_zorder(-10)
pyplot.annotate(
  matplotlib.__version__,
  (0.5, 0.5), (0.5, 0.5), 'axes fraction', 'axes fraction',
  ha='center', va='center')

pyplot.savefig('test.pdf')

Actual outcome

Traceback (most recent call last):
  File "test.py", line 23, in <module>
    pyplot.savefig('test.pdf')
  File "/home/edwin/matplotlib/lib/matplotlib/pyplot.py", line 954, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/home/edwin/matplotlib/lib/matplotlib/figure.py", line 3273, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/home/edwin/matplotlib/lib/matplotlib/backend_bases.py", line 2357, in print_figure
    result = print_method(
  File "/home/edwin/matplotlib/lib/matplotlib/backend_bases.py", line 2223, in <lambda>
    print_method = functools.wraps(meth)(lambda *args, **kwargs: meth(
  File "/home/edwin/matplotlib/lib/matplotlib/backends/backend_pdf.py", line 2815, in print_pdf
    self.figure.draw(renderer)
  File "/home/edwin/matplotlib/lib/matplotlib/artist.py", line 74, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "/home/edwin/matplotlib/lib/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer)
  File "/home/edwin/matplotlib/lib/matplotlib/figure.py", line 3070, in draw
    mimage._draw_list_compositing_images(
  File "/home/edwin/matplotlib/lib/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/edwin/matplotlib/lib/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer)
  File "/home/edwin/matplotlib/lib/matplotlib/axes/_base.py", line 3151, in draw
    mimage._draw_list_compositing_images(
  File "/home/edwin/matplotlib/lib/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "/home/edwin/matplotlib/lib/matplotlib/artist.py", line 45, in draw_wrapper
    renderer.stop_rasterizing()
  File "/home/edwin/matplotlib/lib/matplotlib/backends/backend_mixed.py", line 97, in stop_rasterizing
    img = np.asarray(self._raster_renderer.buffer_rgba())
AttributeError: 'NoneType' object has no attribute 'buffer_rgba'

Expected outcome

Additional information

The bug appears in version 3.5.1 and commit 2d18bba, but not version 3.1.2.

The most immediate cause seems to be reentrance tracking being dropped from MixedModeRenderer.start_rasterizing() and MixedModeRenderer.stop_rasterizing() in commit b6a2739:

self._raster_renderer = self._raster_renderer_class(
self._width*self.dpi, self._height*self.dpi, self.dpi)

self._raster_renderer = None

However, these are probably not the right places to fix this bug.

Operating system

Ubuntu 20.04, 22.04

Matplotlib Version

3.1.2, 3.5.1, 3.7.0.dev447+g2d18bba0ea

Matplotlib Backend

agg

Python version

3.8.10, 3.10.6

Jupyter version

No response

Installation

git checkout

xerus added a commit to xerus/matplotlib that referenced this issue Oct 28, 2022
As mentionedd in 3.4.0 api changes behaviour.rst the tracking should
be handled there.

This commit avoids calling stop_rasterizing method twice in row making
it throw an exception.

Fixes matplotlib#24235
@tacaswell tacaswell added this to the v3.6.3 milestone Oct 28, 2022
@tacaswell
Copy link
Member

This is not a pcolormesh issue, it is generic to an artist being rasterized due to both zorder and explicitly being set be rasterized

import numpy
import matplotlib
from matplotlib import pyplot as plt

fig, ax = plt.subplots()
ax.set_rasterization_zorder(5)
ln, = ax.plot(range(5), rasterized=True, zorder=1)

fig.savefig('test.pdf')

is a simpler reproducer.

tacaswell added a commit to tacaswell/matplotlib that referenced this issue Dec 18, 2022
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
@QuLogic QuLogic modified the milestones: v3.6.3, v3.7.0 Dec 22, 2022
raphaelquast pushed a commit to raphaelquast/matplotlib that referenced this issue Mar 16, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants