Closed
Description
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:
matplotlib/lib/matplotlib/backends/backend_mixed.py
Lines 87 to 88 in b6a2739
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