10000 Added support for providing 1 or 2 extra colours to the contour routi… · matplotlib/matplotlib@a736819 · GitHub
[go: up one dir, main page]

Skip to content

Commit a736819

Browse files
committed
Added support for providing 1 or 2 extra colours to the contour routines to easily specify the under and over colors.
1 parent 50508af commit a736819

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

lib/matplotlib/contour.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,28 @@ def __init__(self, ax, *args, **kwargs):
864864
ncolors = len(self.levels)
865865
if self.filled:
866866
ncolors -= 1
867-
cmap = colors.ListedColormap(self.colors, N=ncolors)
867+
868+
# Handle the case where colors are given for the extended
869+
# parts of the contour.
870+
given_colors = self.colors
871+
extend_min = self.extend in ['min', 'both']
872+
extend_max = self.extend in ['max', 'both']
873+
use_set_under_over = False
874+
# if we are extending the lower end, and we've been given enough colors
875+
# then skip the first color in the resulting cmap.
876+
total_levels = ncolors + int(extend_min) + int(extend_max)
877+
if len(self.colors) == total_levels and any([extend_min, extend_max]):
878+
use_set_under_over = True
879+
if extend_min:
880+
given_colors = given_colors[1:]
881+
882+
cmap = colors.ListedColormap(given_colors, N=ncolors)
883+
if use_set_under_over:
884+
if extend_min:
885+
cmap.set_under(self.colors[0])
886+
if extend_max:
887+
cmap.set_over(self.colors[-1])
888+
868889
if self.filled:
869890
self.collections = cbook.silent_list('mcoll.PathCollection')
870891
else:
@@ -1172,16 +1193,16 @@ def _process_levels(self):
11721193
# (Colorbar needs this even for line contours.)
11731194
self._levels = list(self.levels)
11741195

1175-
if not self.filled:
1176-
self.layers = self.levels
1177-
return
1178-
11791196
if self.extend in ('both', 'min'):
11801197
self._levels.insert(0, min(self.levels[0], self.zmin) - 1)
11811198
if self.extend in ('both', 'max'):
11821199
self._levels.append(max(self.levels[-1], self.zmax) + 1)
11831200
self._levels = np.asarray(self._levels)
11841201

1202+
if not self.filled:
1203+
self.layers = self.levels
1204+
return
1205+
11851206
# layer values are mid-way between levels
11861207
self.layers = 0.5 * (self._levels[:-1] + self._levels[1:])
11871208
# ...except that extended layers must be outside the
@@ -1526,9 +1547,7 @@ def _check_xyz(self, args, kwargs):
15261547
if y.shape != z.shape:
15271548
raise TypeError("Shape of y does not match that of z: found "
15281549
"{0} instead of {1}.".format(y.shape, z.shape))
1529-
15301550
else:
1531-
15321551
raise TypeError("Inputs x and y must be 1D or 2D.")
15331552

15341553
return x, y, z
Loading

lib/matplotlib/tests/test_contour.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,34 @@ def test_contour_manual_labels():
149149
cs = plt.contour(x,y,z)
150150
pts = np.array([(1.5, 3.0), (1.5, 4.4), (1.5, 6.0)])
151151
plt.clabel(cs, manual=pts)
152+
153+
154+
@image_comparison(baseline_images=['contour_manual_colors_and_levels'],
155+
extensions=['png'], remove_text=True)
156+
def test_given_colors_levels_and_extends():
157+
_, axes = plt.subplots(2, 4)
158+
159+
data = np.arange(12).reshape(3, 4)
160+
161+
colors = ['red', 'yellow', 'pink', 'blue', 'black']
162+
levels = [2, 4, 8, 10]
163+
164+
for i, ax in enumerate(axes.flatten()):
165+
plt.sca(ax)
166+
167+
filled = i % 2 == 0.
168+
extend = ['neither', 'min', 'max', 'both'][i // 2]
169+
170+
if filled:
171+
last_color = -1 if extend in ['min', 'max'] else None
172+
plt.contourf(data, colors=colors[:last_color], levels=levels, extend=extend)
173+
else:
174+
last_level = -1 if extend == 'both' else None
175+
plt.contour(data, colors=colors, levels=levels[:last_level], extend=extend)
176+
177+
plt.colorbar()
178+
179+
180+
if __name__ == '__main__':
181+
import nose
182+
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)
0