8000 Add `rasterized` option to `contourf` (#29582) · thedatacurious/matplotlib@73d28ee · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 73d28ee

Browse files
Add rasterized option to contourf (matplotlib#29582)
* allow rasterization * use rasterized kwarg * add to documentation * Fix copied docstring Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com> * first pass at test * add image comparison --------- Co-authored-by: Ruth Comer <10599679+rcomer@users.noreply.github.com>
1 parent 3edda65 commit 73d28ee

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

lib/matplotlib/contour.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import matplotlib.cbook as cbook
2525
import matplotlib.patches as mpatches
2626
import matplotlib.transforms as mtransforms
27+
from . import artist
2728

2829

2930
def _contour_labeler_event_handler(cs, inline, inline_spacing, event):
@@ -767,6 +768,7 @@ def __init__(self, ax, *args,
767768
edgecolor="none",
768769
# Default zorder taken from Collection
769770
zorder=kwargs.pop("zorder", 1),
771+
rasterized=kwargs.pop("rasterized", False),
770772
)
771773

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

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

1259+
@artist.allow_rasterization
12571260
def draw(self, renderer):
12581261
paths = self._paths
12591262
n_paths = len(paths)
@@ -1681,6 +1684,13 @@ def _initialize_x_y(self, z):
16811684
data : indexable object, optional
16821685
DATA_PARAMETER_PLACEHOLDER
16831686
1687+
rasterized : bool, optional
1688+
*Only applies to* `.contourf`.
1689+
Rasterize the contour plot when drawing vector graphics. This can
1690+
speed up rendering and produce smaller files for large data sets.
1691+
See also :doc:`/gallery/misc/rasterization_demo`.
1692+
1693+
16841694
Notes
16851695
-----
16861696
1. `.contourf` differs from the MATLAB version in that it does not draw
Binary file not shown.

lib/matplotlib/tests/test_contour.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,3 +837,13 @@ def test_allsegs_allkinds():
837837
assert len(result) == 2
838838
assert len(result[0]) == 5
839839
assert len(result[1]) == 4
840+
841+
842+
@image_comparison(baseline_images=['contour_rasterization'],
843+
extensions=['pdf'], style='mpl20', savefig_kwarg={'dpi': 25})
844+
def test_contourf_rasterize():
845+
fig, ax = plt.subplots()
846+
data = [[0, 1], [1, 0]]
847+
circle = mpatches.Circle([0.5, 0.5], 0.5, transform=ax.transAxes)
848+
cs = ax.contourf(data, clip_path=circle, rasterized=True)
849+
assert cs._rasterized

0 commit comments

Comments
 (0)
0