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

Skip to content

Add rasterized option to contourf #29582

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 8 commits into from
Mar 18, 2025

Conversation

AdamOrmondroyd
Copy link
Contributor

@AdamOrmondroyd AdamOrmondroyd commented Feb 5, 2025

PR summary

Hi, this is my first PR to matplotlib!

The rasterization documentation suggests one can add rasterized=True to contourf, similar to pcolormesh. This doesn't do anything besides warn the user the kwarg hasn't been used. Setting the resulting QuadContourSet to rasterised causes another warning, but seems to work fine. #27669 picks up on this.

This PR addresses both issues, still need to add tests.

import matplotlib.pyplot as plt
import numpy as np

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

fig, ax = plt.subplots(layout='constrained')

CS = ax.contourf(X, Y, Z, 10, rasterized=True)
# currently causes UserWarning: The following kwargs were not used by contour: 'rasterized'
# result isn't rasterized

# or
CS = plt.contourf(X, Y, Z, 10)
CS.set_rasterized(True)
# currently causes UserWarning: Rasterization of '<matplotlib.contour.QuadContourSet object at 0x10304e3c0>' will be ignored
# result is rasterized
fig.savefig('foo.pdf')

(screenshot of) unrasterized foo.pdf
Screenshot 2025-02-05 at 19 16 54
(screenshot of) rasterized foo.pdf
Screenshot 2025-02-05 at 19 15 38

Closes #27669

PR checklist

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@AdamOrmondroyd
Copy link
Contributor Author

I've had a good look and can't see where the test for this would naturally fit in

@AdamOrmondroyd AdamOrmondroyd marked this pull request as ready for review February 9, 2025 14:29
Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @AdamOrmondroyd and sorry you didn't get a response sooner. I think test_contour.py would be a reasonable place to add a test.

@rcomer rcomer moved this from Needs review to Waiting for author in First Time Contributors Mar 3, 2025
@tacaswell tacaswell added this to the v3.11.0 milestone Mar 17, 2025
@tacaswell
Copy link
Member

I agree on both apologizing that this did not get attention earlier and adding a baseline image.

It is a surprising oversight that this has been missing!

@AdamOrmondroyd
Copy link
Contributor Author

Seems to work (screenshot of contour_rasterization.pdf with dpi set to 25dpi)

Screenshot 2025-03-17 at 10 10 16

What tolerance is appropriate for image_comparison?

@rcomer
Copy link
Member

rcomer commented Mar 17, 2025

Usually leaving the tolerance to default is fine. Sometimes it needs to be a little higher to accommodate slightly different results on different platforms. So you can just wait to see if CI passes.

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good thanks!

@AdamOrmondroyd
Copy link
Contributor Author

do I need access? I don't have a merge button

@rcomer rcomer merged commit 73d28ee into matplotlib:main Mar 18, 2025
41 checks passed
@rcomer
Copy link
Member

rcomer commented Mar 18, 2025

do I need access? I don't have a merge button

No, we just require two approving reviews before merging code changes.

Thanks for your work on this @AdamOrmondroyd and congratulations on your first Matplotlib PR! We hope to hear from you again.

@QuLogic QuLogic moved this from Waiting for author to Merged in First Time Contributors Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

[Doc]: documentation of how to properly rasterize output of contourf
4 participants