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

Skip to content

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

Closed
@EdwinChan

Description

@EdwinChan

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions