Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afda25a

Browse files
authored
Merge pull request #12086 from jklymak/fix-changing-orderofmagnitude
FIX: make MaxNLocator only follow visible ticks for order of magnitude
2 parents 4eee751 + 9a23d6d commit afda25a

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,17 @@ class TestScalarFormatter(object):
275275

276276
use_offset_data = [True, False]
277277

278+
# (sci_type, scilimits, lim, orderOfMag, fewticks)
278279
scilimits_data = [
279-
(False, (0, 0), (10.0, 20.0), 0),
280-
(True, (-2, 2), (-10, 20), 0),
281-
(True, (-2, 2), (-20, 10), 0),
282-
(True, (-2, 2), (-110, 120), 2),
283-
(True, (-2, 2), (-120, 110), 2),
284-
(True, (-2, 2), (-.001, 0.002), -3),
285-
(True, (0, 0), (-1e5, 1e5), 5),
286-
(True, (6, 6), (-1e5, 1e5), 6),
280+
(False, (0, 0), (10.0, 20.0), 0, False),
281+
(True, (-2, 2), (-10, 20), 0, False),
282+
(True, (-2, 2), (-20, 10), 0, False),
283+
(True, (-2, 2), (-110, 120), 2, False),
284+
(True, (-2, 2), (-120, 110), 2, False),
285+
(True, (-2, 2), (-.001, 0.002), -3, False),
286+
(True, (-7, 7), (0.18e10, 0.83e10), 9, True),
287+
(True, (0, 0), (-1e5, 1e5), 5, False),
288+
(True, (6, 6), (-1e5, 1e5), 6, False),
287289
]
288290

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

318320
@pytest.mark.parametrize(
319-
'sci_type, scilimits, lim, orderOfMag', scilimits_data)
320-
def test_scilimits(self, sci_type, scilimits, lim, orderOfMag):
321+
'sci_type, scilimits, lim, orderOfMag, fewticks', scilimits_data)
322+
def test_scilimits(self, sci_type, scilimits, lim, orderOfMag,
323+
fewticks):
321324
tmp_form = mticker.ScalarFormatter()
322325
tmp_form.set_scientific(sci_type)
323326
tmp_form.set_powerlimits(scilimits)
324327
fig, ax = plt.subplots()
325328
ax.yaxis.set_major_formatter(tmp_form)
326329
ax.set_ylim(*lim)
330+
if fewticks:
331+
ax.yaxis.set_major_locator(mticker.MaxNLocator(4))
332+
327333
tmp_form.set_locs(ax.yaxis.get_majorticklocs())
328334
assert orderOfMag == tmp_form.orderOfMagnitude
329335

lib/matplotlib/ticker.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,14 @@ def _set_orderOfMagnitude(self, range):
712712
# fixed scaling when lower power limit = upper <> 0.
713713
self.orderOfMagnitude = self._powerlimits[0]
714714
return
715-
locs = np.abs(self.locs)
715+
# restrict to visible ticks
716+
vmin, vmax = sorted(self.axis.get_view_interval())
717+
locs = np.asarray(self.locs)
718+
locs = locs[(vmin <= locs) & (locs <= vmax)]
719+
locs = np.abs(locs)
720+
if not len(locs):
721+
self.orderOfMagnitude = 0
722+
return
716723
if self.offset:
717724
oom = math.floor(math.log10(range))
718725
else:

0 commit comments

Comments
 (0)