Skip to content

Commit 37bce68

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 4bdae2e commit 37bce68

File tree

20 files changed

+557
-403
lines changed

20 files changed

+557
-403
lines changed
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.

galleries/examples/images_contours_and_fields/contour_demo.py

+3-1
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.resize(CS.get_linewidth(), len(levels))
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)

galleries/examples/images_contours_and_fields/contour_image.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@
5656

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

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

galleries/examples/images_contours_and_fields/contours_in_optimization_demo.py

+3-7
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 matplotlib.pyplot as plt
@@ -48,16 +47,13 @@
4847
ax.clabel(cntr, fmt="%2.1f", use_clabeltext=True)
4948

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

5452
cg2 = ax.contour(x1, x2, g2, [0], colors='orangered')
55-
plt.setp(cg2.collections,
56-
path_effects=[patheffects.withTickedStroke(angle=60, length=2)])
53+
cg2.set(path_effects=[patheffects.withTickedStroke(angle=60, length=2)])
5754

5855
cg3 = ax.contour(x1, x2, g3, [0], colors='mediumblue')
59-
plt.setp(cg3.collections,
60-
path_effects=[patheffects.withTickedStroke(spacing=7)])
56+
cg3.set(path_effects=[patheffects.withTickedStroke(spacing=7)])
6157

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

galleries/examples/misc/patheffect_demo.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
ax2.imshow(arr)
3030
cntr = ax2.contour(arr, colors="k")
3131

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

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

galleries/examples/misc/tickedstroke_demo.py

+3-6
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

+3-9
Original file line numberDiff line numberDiff line change
@@ -2171,15 +2171,9 @@ def _sci(self, im):
21712171
``pyplot.viridis``, and other functions such as `~.pyplot.clim`. The
21722172
current image is an attribute of the current Axes.
21732173
"""
2174-
_api.check_isinstance(
2175-
(mpl.contour.ContourSet, mcoll.Collection, mimage.AxesImage),
2176-
im=im)
2177-
if isinstance(im, mpl.contour.ContourSet):
2178-
if im.collections[0] not in self._children:
2179-
raise ValueError("ContourSet must be in current Axes")
2180-
elif im not in self._children:
2181-
raise ValueError("Argument must be an image, collection, or "
2182-
"ContourSet in this Axes")
2174+
_api.check_isinstance((mcoll.Collection, mimage.AxesImage), im=im)
2175+
if im not in self._children:
2176+
raise ValueError("Argument must be an image or collection in this Axes")
21832177
self._current_image = im
21842178

21852179
def _gci(self):

lib/matplotlib/colorbar.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -750,15 +750,15 @@ def add_lines(self, *args, **kwargs):
750750
lambda self, levels, colors, linewidths, erase=True: locals()],
751751
self, *args, **kwargs)
752752
if "CS" in params:
753-
self, CS, erase = params.values()
754-
if not isinstance(CS, contour.ContourSet) or CS.filled:
753+
self, cs, erase = params.values()
754+
if not isinstance(cs, contour.ContourSet) or cs.filled:
755755
raise ValueError("If a single artist is passed to add_lines, "
756756
"it must be a ContourSet of lines")
757757
# TODO: Make colorbar lines auto-follow changes in contour lines.
758758
return self.add_lines(
759-
CS.levels,
760-
CS.to_rgba(CS.cvalues, CS.alpha),
761-
[coll.get_linewidths()[0] for coll in CS.collections],
759+
cs.levels,
760+
cs.to_rgba(cs.cvalues, cs.alpha),
761+
cs.get_linewidths(),
762762
erase=erase)
763763
else:
764764
self, levels, colors, linewidths, erase = params.values()

0 commit comments

Comments
 (0)