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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import matplotlib.cbook as cbook
import matplotlib.patches as mpatches
import matplotlib.transforms as mtransforms
from . import artist


def _contour_labeler_event_handler(cs, inline, inline_spacing, event):
Expand Down Expand Up @@ -767,6 +768,7 @@ def __init__(self, ax, *args,
edgecolor="none",
# Default zorder taken from Collection
zorder=kwargs.pop("zorder", 1),
rasterized=kwargs.pop("rasterized", False),
)

else:
Expand Down Expand Up @@ -1254,6 +1256,7 @@ def find_nearest_contour(self, x, y, indices=None, pixel=True):

return (i_level, segment, index, xmin, ymin, d2)

@artist.allow_rasterization
def draw(self, renderer):
paths = self._paths
n_paths = len(paths)
Expand Down Expand Up @@ -1681,6 +1684,13 @@ def _initialize_x_y(self, z):
data : indexable object, optional
DATA_PARAMETER_PLACEHOLDER

rasterized : bool, optional
*Only applies to* `.contourf`.
Rasterize the contour plot when drawing vector graphics. This can
speed up rendering and produce smaller files for large data sets.
See also :doc:`/gallery/misc/rasterization_demo`.


Notes
-----
1. `.contourf` differs from the MATLAB version in that it does not draw
Expand Down
Binary file not shown.
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,13 @@ def test_allsegs_allkinds():
assert len(result) == 2
assert len(result[0]) == 5
assert len(result[1]) == 4


@image_comparison(baseline_images=['contour_rasterization'],
extensions=['pdf'], style='mpl20', savefig_kwarg={'dpi': 25})
def test_contourf_rasterize():
fig, ax = plt.subplots()
data = [[0, 1], [1, 0]]
circle = mpatches.Circle([0.5, 0.5], 0.5, transform=ax.transAxes)
cs = ax.contourf(data, clip_path=circle, rasterized=True)
assert cs._rasterized
Loading