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

Skip to content

Commit e9252ca

Browse files
committed
FIX: try hard to ensure at least 2 ticks with 'auto'
This requires falling back on a larger set of options for intervals between ticks. Closes #5784.
1 parent 46d6e6c commit e9252ca

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

lib/matplotlib/ticker.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,8 @@ def __init__(self, *args, **kwargs):
16191619
will be removed. If prune==None, no ticks will be removed.
16201620
16211621
"""
1622+
self._fine_steps = [1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10]
1623+
self._fine_int_steps = [1, 2, 3, 4, 5, 6, 8, 10]
16221624
if args:
16231625
kwargs['nbins'] = args[0]
16241626
if len(args) > 1:
@@ -1631,8 +1633,11 @@ def set_params(self, **kwargs):
16311633
"""Set parameters within this locator."""
16321634
if 'nbins' in kwargs:
16331635
self._nbins = kwargs['nbins']
1634-
if self._nbins != 'auto':
1635-
self._nbins = int(self._nbins)
1636+
if self._nbins != 'auto':
1637+
self._nbins = int(self._nbins)
1638+
self._steps2 = None
1639+
else:
1640+
self._steps2 = self._fine_steps
16361641
if 'trim' in kwargs:
16371642
warnings.warn(
16381643
"The 'trim' keyword has no effect since version 2.0.",
@@ -1650,7 +1655,8 @@ def set_params(self, **kwargs):
16501655
if 'steps' in kwargs:
16511656
steps = kwargs['steps']
16521657
if steps is None:
1653-
self._steps = [1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10]
1658+
self._steps = self._fine_steps
1659+
self._steps2 = None
16541660
else:
16551661
if int(steps[-1]) != 10:
16561662
steps = list(steps)
@@ -1660,11 +1666,15 @@ def set_params(self, **kwargs):
16601666
self._integer = kwargs['integer']
16611667
if self._integer:
16621668
self._steps = [n for n in self._steps if _divmod(n, 1)[1] < 0.001]
1669+
if self._steps2 is not None:
1670+
self._steps2 = self._fine_int_steps
16631671

1664-
def _raw_ticks(self, vmin, vmax):
1672+
def _raw_ticks(self, vmin, vmax, steps=None):
1673+
if steps is None:
1674+
steps = self._steps
16651675
nbins = self._nbins
16661676
if nbins == 'auto':
1667-
nbins = max(min(self.axis.get_tick_space(), 9), 1)
1677+
nbins = max(min(self.axis.get_tick_space(), 9), 2)
16681678
scale, offset = scale_range(vmin, vmax, nbins)
16691679
if self._integer:
16701680
scale = max(1, scale)
@@ -1675,7 +1685,7 @@ def _raw_ticks(self, vmin, vmax):
16751685
best_vmax = vmax
16761686
best_vmin = vmin
16771687

1678-
for step in self._steps:
1688+
for step in steps:
16791689
if step < scaled_raw_step:
16801690
continue
16811691
step *= scale
@@ -1703,7 +1713,14 @@ def tick_values(self, vmin, vmax):
17031713
vmin, vmax = mtransforms.nonsingular(
17041714
vmin, vmax, expander=1e-13, tiny=1e-14)
17051715
locs = self._raw_ticks(vmin, vmax)
1716+
nlocs = len(locs)
17061717
prune = self._prune
1718+
if prune in ('lower', 'upper'):
1719+
nlocs -= 1
1720+
elif prune == 'both':
1721+
nlocs -= 2
1722+
if self._steps2 and nlocs < 2:
1723+
locs = self._raw_ticks(vmin, vmax, self._steps2)
17071724
if prune == 'lower':
17081725
locs = locs[1:]
17091726
elif prune == 'upper':

0 commit comments

Comments
 (0)