Skip to content

Uptade fix number of levels with log contour #29137

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 21 additions & 6 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,13 +960,12 @@
label.set_color(self.labelMappable.to_rgba(cv))
super().changed()

def _autolev(self, N):
def _autolev(self, N, *, using_default):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this extra argument is not necessary.

The only place where this is passed it is simply a N is not None (on the input side, but the effect is the same)

So even if the variable is useful, it can be computed internally.

However, even further, the difference between LogLocator() and LogLocator(numticks=N) when N is None is nothing, therefore you can just pass it as a kwarg.

Haven't fully thought through the last portion (adjustments if levels are still insufficient) and if there may need to be at least a if N is not None in there potentially, but not sure yet.

"""
Select contour levels to span the data.

The target number of levels, *N*, is used only when the
scale is not log and default locator is used.

locator is not set and the scale is log or the default
locator is used.

Check warning on line 968 in lib/matplotlib/contour.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/contour.py#L967-L968

Added lines #L967 - L968 were not covered by tests
We need two more levels for filled contours than for
line contours, because for the latter we need to specify
the lower and upper boundary of each range. For example,
Expand All @@ -976,7 +975,12 @@
"""
if self.locator is None:
if self.logscale:
self.locator = ticker.LogLocator()
if using_default:
# Let log locator choose instead of using hard coded value
# set in self._process_contour_level_args()
self.locator = ticker.LogLocator()
else:
self.locator = ticker.LogLocator(numticks=N)
else:
self.locator = ticker.MaxNLocator(N + 1, min_n_ticks=1)

Expand All @@ -993,14 +997,25 @@
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

# Ensure at least 3 levels and handle log scale specifically
if i1 - i0 < 3:
i0, i1 = 0, len(lev)

# Handle log scale adjustments if levels are still insufficient
if self.logscale and len(lev) < N:
self.locator = ticker.LogLocator(numticks=N+1)
lev = self.locator.tick_values(self.zmin, self.zmax)
under = np.nonzero(lev < self.zmin)[0]
i0 = under[-1] if len(under) else 0
over = np.nonzero(lev > self.zmax)[0]

Check warning on line 1016 in lib/matplotlib/contour.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/contour.py#L1016

Added line #L1016 was not covered by tests
i1 = over[0] + 1 if len(over) else len(lev)

return lev[i0:i1]

def _process_contour_level_args(self, args, z_dtype):
Expand All @@ -1020,7 +1035,7 @@
else:
levels_arg = self.levels
if isinstance(levels_arg, Integral):
self.levels = self._autolev(levels_arg)
self.levels = self._autolev(levels_arg, using_default=self.levels is None)
else:
self.levels = np.asarray(levels_arg, np.float64)
if self.filled and len(self.levels) < 2:
Expand Down
Loading