Skip to content

WIP: Contour log extension #8834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,15 +1180,34 @@ 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()
else:
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):
"""
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down