-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Make LogLocator only return one tick out of range #24436
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
Changes from all commits
d495301
5e733de
4ba7411
737aa02
36adb34
792236a
a8ba49d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Improved tick location logic in ``LogLocator`` | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
`~.LogLocator.tick_values` previously returned two ticks that were outside | ||
the range ``{vmin, vmax}``. This has been updated to only return all ticks | ||
for the decades containing ``vmin`` and ``vmax``. If ``vmin`` or ``vmax`` | ||
are exactly on a decade this means no ticks above/below are returned. | ||
Otherwise a single major tick is returned above/below, and for minor ticks | ||
all the ticks in the decade containing ``vmin`` or ``vmax`` are returned. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -493,10 +493,10 @@ def test_colorbar_autotickslog(): | |
orientation='vertical', shrink=0.4) | ||
# note only -12 to +12 are visible | ||
np.testing.assert_almost_equal(cbar.ax.yaxis.get_ticklocs(), | ||
10**np.arange(-16., 16.2, 4.)) | ||
# note only -24 to +24 are visible | ||
10**np.arange(-12., 12.2, 4.)) | ||
# note only -12 to +12 are visible | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this comment wrong before? |
||
np.testing.assert_almost_equal(cbar2.ax.yaxis.get_ticklocs(), | ||
10**np.arange(-24., 25., 12.)) | ||
10**np.arange(-12., 13., 12.)) | ||
|
||
|
||
def test_colorbar_get_ticks(): | ||
|
@@ -584,6 +584,7 @@ def test_colorbar_log_minortick_labels(): | |
def test_colorbar_renorm(): | ||
x, y = np.ogrid[-4:4:31j, -4:4:31j] | ||
z = 120000*np.exp(-x**2 - y**2) | ||
# min/max of z vals is 1.5196998658913011e-09, 120000.0 | ||
|
||
fig, ax = plt.subplots() | ||
im = ax.imshow(z) | ||
|
@@ -597,7 +598,7 @@ def test_colorbar_renorm(): | |
norm = LogNorm(z.min(), z.max()) | ||
im.set_norm(norm) | ||
np.testing.assert_allclose(cbar.ax.yaxis.get_majorticklocs(), | ||
np.logspace(-10, 7, 18)) | ||
np.logspace(-9, 6, 16)) | ||
# note that set_norm removes the FixedLocator... | ||
assert np.isclose(cbar.vmin, z.min()) | ||
cbar.set_ticks([1, 2, 3]) | ||
|
@@ -616,6 +617,7 @@ def test_colorbar_format(fmt): | |
# make sure that format is passed properly | ||
x, y = np.ogrid[-4:4:31j, -4:4:31j] | ||
z = 120000*np.exp(-x**2 - y**2) | ||
# min/max of z vals is 1.5196998658913011e-09, 120000.0 | ||
|
||
fig, ax = plt.subplots() | ||
im = ax.imshow(z) | ||
|
@@ -633,7 +635,7 @@ def test_colorbar_format(fmt): | |
im.set_norm(LogNorm(vmin=0.1, vmax=10)) | ||
fig.canvas.draw() | ||
assert (cbar.ax.yaxis.get_ticklabels()[0].get_text() == | ||
'$\\mathdefault{10^{-2}}$') | ||
'$\\mathdefault{10^{-1}}$') | ||
|
||
|
||
def test_colorbar_scale_reset(): | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2344,6 +2344,15 @@ def __call__(self): | |||||
return self.tick_values(vmin, vmax) | ||||||
|
||||||
def tick_values(self, vmin, vmax): | ||||||
""" | ||||||
Return the values of the located ticks given **vmin** and **vmax**. | ||||||
|
||||||
Notes | ||||||
----- | ||||||
The tick values returned include all ticks for the decades containing | ||||||
``vmin`` and ``vmax``, which can result in some tick values below | ||||||
``vmin`` and above ``vmax``. | ||||||
""" | ||||||
if self.numticks == 'auto': | ||||||
if self.axis is not None: | ||||||
numticks = np.clip(self.axis.get_tick_space(), 2, 9) | ||||||
|
@@ -2359,7 +2368,7 @@ def tick_values(self, vmin, vmax): | |||||
|
||||||
if vmin <= 0.0 or not np.isfinite(vmin): | ||||||
raise ValueError( | ||||||
"Data has no positive values, and therefore can not be " | ||||||
"Data has non-positive values, and therefore can not be " | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This overall logic does not seem 10% correct because we are only checking vmin here and just below we ensure that vmin and vmax are sorted. We also only get this error if the locator is not attached to as axis as in that case we silently clip vmin. |
||||||
"log-scaled.") | ||||||
|
||||||
_log.debug('vmin %s vmax %s', vmin, vmax) | ||||||
|
@@ -2399,8 +2408,10 @@ def tick_values(self, vmin, vmax): | |||||
# whether we're a major or a minor locator. | ||||||
have_subs = len(subs) > 1 or (len(subs) == 1 and subs[0] != 1.0) | ||||||
|
||||||
decades = np.arange(math.floor(log_vmin) - stride, | ||||||
math.ceil(log_vmax) + 2 * stride, stride) | ||||||
# Return one tick below (or equal to) vmin, | ||||||
# and one tick above (or equal to) vmax | ||||||
decades = np.arange(math.floor(log_vmin), | ||||||
math.ceil(log_vmax) + stride, stride) | ||||||
|
||||||
if hasattr(self, '_transform'): | ||||||
ticklocs = self._transform.inverted().transform(decades) | ||||||
|
@@ -2450,7 +2461,7 @@ def nonsingular(self, vmin, vmax): | |||||
vmin, vmax = 1, 10 # Initial range, no data plotted yet. | ||||||
elif vmax <= 0: | ||||||
_api.warn_external( | ||||||
"Data has no positive values, and therefore cannot be " | ||||||
"Data has non-positive values, and therefore cannot be " | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this one should stay as "no positive" as in the other blocks we clip vmin to a small positive value. |
||||||
"log-scaled.") | ||||||
vmin, vmax = 1, 10 | ||||||
else: | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this clarify the previous behaviour? Did we un-conditionally return an extra tick?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before it was 1 or 2, now it is 0 or 1.
I think the question is MUST locators return extra or MAY locators return extra.