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

Skip to content

[Doc]: documentation of how to properly rasterize output of contourf #27669

Closed
@tarrla

Description

@tarrla

Documentation Link

https://matplotlib.org/stable/gallery/images_contours_and_fields/contourf_demo.html

Problem

The contourf demo could use a clear example of how to properly rasterize the output. It may be useful to include this information directly in the contourf documentation.

Here's a minimal example that shows what used to work for me (based on the example in the contourf demo and the top answer from stackoverflow), but is now deprecated as of matplotlib 3.8:

import numpy as np
import matplotlib.pyplot as plt
delta = 0.025

x = y = np.arange(-3.0, 3.01, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2

fig, ax = plt.subplots()

cont = ax.contourf(X,Y,Z, levels = 100)

for c in cont.collections:
    c.set_rasterized(True)

This now returns:

MatplotlibDeprecationWarning: The collections attribute was deprecated in Matplotlib 3.8 and will be removed two minor releases later.
  for c in cont.collections:

It's not obvious to me how I should be rasterizing a contourf for matplotlib>=3.8 (regardless of whether the method I used above was sensical or not...at least it worked).

If I try to apply something similar to what is in the current rasterization demo, (which I think resulted from #17708 ),

fig2, ax2 = plt.subplots()
cont = ax2.contourf(X,Y,Z, levels = 100, rasterized=True)

then I get the following error:

UserWarning: The following kwargs were not used by contour: 'rasterized'
  cont = ax2.contourf(X,Y,Z, levels = 100, rasterized=True)

Suggested improvement

  1. Include a paragraph and code snippet somewhere in https://matplotlib.org/stable/gallery/images_contours_and_fields/contourf_demo.html explaining how to rasterize contourf.

  2. maybe include a description of how to rasterize directly in the documentation of contourf

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions