Skip to content

Commit 166a144

Browse files
authored
Merge pull request #8737 from dstansby/fix-tests
Fix colorbar test and color level determination for contour
2 parents aec1e5b + 73e2c0f commit 166a144

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

lib/matplotlib/contour.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1254,11 +1254,11 @@ def _process_colors(self):
12541254
i0, i1 = 0, len(self.levels)
12551255
if self.filled:
12561256
i1 -= 1
1257-
# Out of range indices for over and under:
1258-
if self.extend in ('both', 'min'):
1259-
i0 = -1
1260-
if self.extend in ('both', 'max'):
1261-
i1 += 1
1257+
# Out of range indices for over and under:
1258+
if self.extend in ('both', 'min'):
1259+
i0 -= 1
1260+
if self.extend in ('both', 'max'):
1261+
i1 += 1
12621262
self.cvalues = list(range(i0, i1))
12631263
self.set_norm(colors.NoNorm())
12641264
else:

lib/matplotlib/tests/test_contour.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,22 @@ def test_given_colors_levels_and_extends():
168168
levels = [2, 4, 8, 10]
169169

170170
for i, ax in enumerate(axes.flatten()):
171-
plt.sca(ax)
172-
173171
filled = i % 2 == 0.
174172
extend = ['neither', 'min', 'max', 'both'][i // 2]
175173

176174
if filled:
177-
last_color = -1 if extend in ['min', 'max'] else None
178-
plt.contourf(data, colors=colors[:last_color], levels=levels,
179-
extend=extend)
175+
# If filled, we have 3 colors with no extension,
176+
# 4 colors with one extension, and 5 colors with both extensions
177+
first_color = 1 if extend in ['max', 'neither'] else None
178+
last_color = -1 if extend in ['min', 'neither'] else None
179+
c = ax.contourf(data, colors=colors[first_color:last_color],
180+
levels=levels, extend=extend)
180181
else:
181-
last_level = -1 if extend == 'both' else None
182-
plt.contour(data, colors=colors, levels=levels[:last_level],
183-
extend=extend)
182+
# If not filled, we have 4 levels and 4 colors
183+
c = ax.contour(data, colors=colors[:-1],
184+
levels=levels, extend=extend)
184185

185-
plt.colorbar()
186+
plt.colorbar(c, ax=ax)
186187

187188

188189
@image_comparison(baseline_images=['contour_datetime_axis'],

0 commit comments

Comments
 (0)