diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 8393a4b2b066..c6dfb6bdfcfc 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -1180,6 +1180,7 @@ def _autolev(self, N): one contour line, but two filled regions, and therefore three levels to provide boundaries for both regions. """ + self._auto = True if self.locator is None: if self.logscale: self.locator = ticker.LogLocator() @@ -1187,8 +1188,26 @@ def _autolev(self, N): self.locator = ticker.MaxNLocator(N + 1, min_n_ticks=1) lev = self.locator.tick_values(self.zmin, self.zmax) - self._auto = True - return lev + + try: + if self.locator._symmetric: + return lev + except AttributeError: + pass + + under = np.nonzero(lev < self.zmin)[0] + i0 = under[-1] if len(under) else 0 + over = np.nonzero(lev > self.zmax)[0] + i1 = over[0] + 1 if len(over) else len(lev) + if self.extend in ('min', 'both'): + i0 += 1 + if self.extend in ('max', 'both'): + i1 -= 1 + + if i1 - i0 < 3: + i0, i1 = 0, len(lev) + + return lev[i0:i1] def _contour_level_args(self, z, args): """ @@ -1245,10 +1264,22 @@ def _process_levels(self): # (Colorbar needs this even for line contours.) self._levels = list(self.levels) + if self.logscale: + def raised(x): return x * 1.1 + + def lowered(x): return x / 1.1 + + else: + def raised(x): return x + 1 + + def lowered(x): return x - 1 + if self.extend in ('both', 'min'): - self._levels.insert(0, min(self.levels[0], self.zmin) - 1) + lower = lowered(min(self.levels[0], self.zmin)) + self._levels.insert(0, lower) if self.extend in ('both', 'max'): - self._levels.append(max(self.levels[-1], self.zmax) + 1) + upper = raised(max(self.levels[-1], self.zmax)) + self._levels.append(upper) self._levels = np.asarray(self._levels) if not self.filled: @@ -1260,10 +1291,7 @@ def _process_levels(self): # ...except that extended layers must be outside the # normed range: if self.extend in ('both', 'min'): - if self.logscale: - self.layers[0] = 1e-150 - else: - self.layers[0] = -1e150 + self.layers[0] = 1e-150 if self.logscale else -1e150 if self.extend in ('both', 'max'): self.layers[-1] = 1e150 diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index bd6961b16d75..37a976133d0c 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1647,7 +1647,7 @@ def test_contour_hatching(): fig = plt.figure() ax = fig.add_subplot(111) - cs = ax.contourf(x, y, z, hatches=['-', '/', '\\', '//'], + cs = ax.contourf(x, y, z, hatches=['/', '\\', '//', '-'], cmap=plt.get_cmap('gray'), extend='both', alpha=0.5)