diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 2b3c21b6d45..c3e9a360f6f 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -831,9 +831,6 @@ def __init__(self, ax, *args, **kwargs): self.logscale = True if norm is None: norm = colors.LogNorm() - if self.extend is not 'neither': - raise ValueError('extend kwarg does not work yet with log ' - ' scale') else: self.logscale = False @@ -1127,6 +1124,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() @@ -1134,8 +1132,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): """ @@ -1213,10 +1229,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: @@ -1228,7 +1256,7 @@ def _process_levels(self): # ...except that extended layers must be outside the # normed range: if self.extend in ('both', 'min'): - 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 f1cc67fde7a..cdfe2aa006f 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -1450,7 +1450,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)