Skip to content

Commit 895a60e

Browse files
committed
Turn ContourSet into a standard Collection artist.
Keep (some) backcompat by making access to ContourSet.collection trigger the self-replacement of the ContourSet by the old-style list of PathCollections. The baseline images are slighly shifted, but the new images actually look more correct: - contour_corner_mask_False the old implementation would white out some extra L-shaped areas between masked regions (particularly visible in the diff image). - 3d/contour3d: The order of the "contours" on the panes is a bit arbitrary, but note that previously on the left pane the white (medium) contour was drawn first, then overlaid with the blue (low) contour, then overlaid with the red (high) contour; the new image draws the contours more consistently in the order blue/white/red. - 3d/tricontour: The new draw order of the unfilled contours (on the left) is clearly better, with the highest contour (light green) drawn above the lower one (medium green). Limitations: - 3d contours used to rely on being able to set a different sort_zpos for each contour level; this change gets rid of that. Per the above it's not clear this is actually worse in practice...
1 parent ba55c52 commit 895a60e

File tree

19 files changed

+539
-384
lines changed

19 files changed

+539
-384
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
``ContourSet.collections``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... is deprecated. `.ContourSet` is now implemented as a single `.Collection` of paths,
4+
each path corresponding to a contour level, possibly including multiple unconnected
5+
components.
6+
7+
During the deprecation period, accessing ``ContourSet.collections`` will revert the
8+
current ContourSet instance to the old object layout, with a separate `.PathCollection`
9+
per contour level.

examples/images_contours_and_fields/contour_demo.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@
8585
linewidths=2, extent=(-3, 3, -2, 2))
8686

8787
# Thicken the zero contour.
88-
CS.collections[6].set_linewidth(4)
88+
lws = np.broadcast_to(CS.get_linewidth(), len(levels)).copy()
89+
lws[6] = 4
90+
CS.set_linewidth(lws)
8991

9092
ax.clabel(CS, levels[1::2], # label every second level
9193
inline=True, fmt='%1.1f', fontsize=14)

examples/images_contours_and_fields/contour_image.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@
5555

5656
# We don't really need dashed contour lines to indicate negative
5757
# regions, so let's turn them off.
58-
59-
for c in cset2.collections:
60-
c.set_linestyle('solid')
58+
cset2.set_linestyle('solid')
6159

6260
# It is easier here to make a separate call to contour than
6361
# to set up an array of colors and linewidths.

examples/images_contours_and_fields/contours_in_optimization_demo.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
`~matplotlib.patheffects.TickedStroke` to illustrate a constraint in
1818
a typical optimization problem, the angle should be set between
1919
zero and 180 degrees.
20-
2120
"""
2221

2322
import numpy as np
@@ -47,16 +46,13 @@
4746
ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True)
4847

4948
cg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown')
50-
plt.setp(cg1.collections,
51-
path_effects=[patheffects.withTickedStroke(angle=135)])
49+
cg1.set(path_effects=[patheffects.withTickedStroke(angle=135)])
5250

5351
cg2 = ax.contour(x1, x2, g2, [0], colors='orangered')
54-
plt.setp(cg2.collections,
55-
path_effects=[patheffects.withTickedStroke(angle=60, length=2)])
52+
cg2.set(path_effects=[patheffects.withTickedStroke(angle=60, length=2)])
5653

5754
cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue')
58-
plt.setp(cg3.collections,
59-
path_effects=[patheffects.withTickedStroke(spacing=7)])
55+
cg3.set(path_effects=[patheffects.withTickedStroke(spacing=7)])
6056

6157
ax.set_xlim(0, 4)
6258
ax.set_ylim(0, 4)

examples/misc/patheffect_demo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
ax2.imshow(arr)
2929
cntr = ax2.contour(arr, colors="k")
3030

31-
plt.setp(cntr.collections, path_effects=[
32-
patheffects.withStroke(linewidth=3, foreground="w")])
31+
cntr.set(path_effects=[patheffects.withStroke(linewidth=3, foreground="w")])
3332

3433
clbls = ax2.clabel(cntr, fmt="%2.0f", use_clabeltext=True)
3534
plt.setp(clbls, path_effects=[

examples/misc/tickedstroke_demo.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,13 @@
8888
ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True)
8989

9090
cg1 = ax.contour(x1, x2, g1, [0], colors='sandybrown')
91-
plt.setp(cg1.collections,
92-
path_effects=[patheffects.withTickedStroke(angle=135)])
91+
cg1.set(path_effects=[patheffects.withTickedStroke(angle=135)])
9392

9493
cg2 = ax.contour(x1, x2, g2, [0], colors='orangered')
95-
plt.setp(cg2.collections,
96-
path_effects=[patheffects.withTickedStroke(angle=60, length=2)])
94+
cg2.set(path_effects=[patheffects.withTickedStroke(angle=60, length=2)])
9795

9896
cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue')
99-
plt.setp(cg3.collections,
100-
path_effects=[patheffects.withTickedStroke(spacing=7)])
97+
cg3.set(path_effects=[patheffects.withTickedStroke(spacing=7)])
10198

10299
ax.set_xlim(0, 4)
103100
ax.set_ylim(0, 4)

lib/matplotlib/axes/_base.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,10 +2179,7 @@ def _sci(self, im):
21792179
_api.check_isinstance(
21802180
(mpl.contour.ContourSet, mcoll.Collection, mimage.AxesImage),
21812181
im=im)
2182-
if isinstance(im, mpl.contour.ContourSet):
2183-
if im.collections[0] not in self._children:
2184-
raise ValueError("ContourSet must be in current Axes")
2185-
elif im not in self._children:
2182+
if im not in self._children:
21862183
raise ValueError("Argument must be an image, collection, or "
21872184
"ContourSet in this Axes")
21882185
self._current_image = im

lib/matplotlib/colorbar.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,15 +767,15 @@ def add_lines(self, *args, **kwargs):
767767
lambda self, levels, colors, linewidths, erase=True: locals()],
768768
self, *args, **kwargs)
769769
if "CS" in params:
770-
self, CS, erase = params.values()
771-
if not isinstance(CS, contour.ContourSet) or CS.filled:
770+
self, cs, erase = params.values()
771+
if not isinstance(cs, contour.ContourSet) or cs.filled:
772772
raise ValueError("If a single artist is passed to add_lines, "
773773
"it must be a ContourSet of lines")
774774
# TODO: Make colorbar lines auto-follow changes in contour lines.
775775
return self.add_lines(
776-
CS.levels,
777-
CS.to_rgba(CS.cvalues, CS.alpha),
778-
[coll.get_linewidths()[0] for coll in CS.collections],
776+
cs.levels,
777+
cs.to_rgba(cs.cvalues, cs.alpha),
778+
cs.get_linewidths(),
779779
erase=erase)
780780
else:
781781
self, levels, colors, linewidths, erase = params.values()

0 commit comments

Comments
 (0)