Skip to content

FIX: make MaxNLocator only follow visible ticks for order of magnitude #12086

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

Merged
merged 2 commits into from
Sep 14, 2018
Merged
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
26 changes: 16 additions & 10 deletions lib/matplotlib/tests/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,17 @@ class TestScalarFormatter(object):

use_offset_data = [True, False]

# (sci_type, scilimits, lim, orderOfMag, fewticks)
scilimits_data = [
(False, (0, 0), (10.0, 20.0), 0),
(True, (-2, 2), (-10, 20), 0),
(True, (-2, 2), (-20, 10), 0),
(True, (-2, 2), (-110, 120), 2),
(True, (-2, 2), (-120, 110), 2),
(True, (-2, 2), (-.001, 0.002), -3),
(True, (0, 0), (-1e5, 1e5), 5),
(True, (6, 6), (-1e5, 1e5), 6),
(False, (0, 0), (10.0, 20.0), 0, False),
(True, (-2, 2), (-10, 20), 0, False),
(True, (-2, 2), (-20, 10), 0, False),
(True, (-2, 2), (-110, 120), 2, False),
(True, (-2, 2), (-120, 110), 2, False),
(True, (-2, 2), (-.001, 0.002), -3, False),
(True, (-7, 7), (0.18e10, 0.83e10), 9, True),
(True, (0, 0), (-1e5, 1e5), 5, False),
(True, (6, 6), (-1e5, 1e5), 6, False),
]

@pytest.mark.parametrize('left, right, offset', offset_data)
Expand Down Expand Up @@ -316,14 +318,18 @@ def test_use_offset(self, use_offset):
assert use_offset == tmp_form.get_useOffset()

@pytest.mark.parametrize(
'sci_type, scilimits, lim, orderOfMag', scilimits_data)
def test_scilimits(self, sci_type, scilimits, lim, orderOfMag):
'sci_type, scilimits, lim, orderOfMag, fewticks', scilimits_data)
def test_scilimits(self, sci_type, scilimits, lim, orderOfMag,
fewticks):
tmp_form = mticker.ScalarFormatter()
tmp_form.set_scientific(sci_type)
tmp_form.set_powerlimits(scilimits)
fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(tmp_form)
ax.set_ylim(*lim)
if fewticks:
ax.yaxis.set_major_locator(mticker.MaxNLocator(4))

tmp_form.set_locs(ax.yaxis.get_majorticklocs())
assert orderOfMag == tmp_form.orderOfMagnitude

Expand Down
9 changes: 8 additions & 1 deletion lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,14 @@ def _set_orderOfMagnitude(self, range):
# fixed scaling when lower power limit = upper <> 0.
self.orderOfMagnitude = self._powerlimits[0]
return
locs = np.abs(self.locs)
# restrict to visible ticks
vmin, vmax = sorted(self.axis.get_view_interval())
locs = np.asarray(self.locs)
locs = locs[(vmin <= locs) & (locs <= vmax)]
locs = np.abs(locs)
if not len(locs):
self.orderOfMagnitude = 0
return
if self.offset:
oom = math.floor(math.log10(range))
else:
Expand Down