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

Skip to content

Commit ed7331d

Browse files
committed
ticker.LinearLocator view_limits algorithm improvement closes #6142
1 parent 3cd9c2d commit ed7331d

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
``matplotlib.ticker.LinearLocator`` algorithm update
2+
```````````````````````````
3+
4+
The ``matplotlib.ticker.LinearLocator`` is used to define the range and location
5+
of tickmarks of a plot when the user wants a exact number of ticks.
6+
``LinearLocator`` thus differs from the default locator ``MaxNLocator``, which
7+
does not necessarily return the exact user requested number of ticks.
8+
9+
The view range algorithm in ``matplotlib.ticker.LinearLocator`` has been
10+
changed so that more convenient tick location are chosen. The new algorithm
11+
returns a plot view range that is a multiple of the user requested number of
12+
ticks. This ensures tick marks to be located at whole integers more
13+
constistently. For example, when both y-axis of a``twinx`` plot use
14+
``matplotlib.ticker.LinearLocator`` with the same number of ticks, the grids of
15+
both axis will be properly aligned at convenient tick locations.
16+

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3780,7 +3780,8 @@ def twinx(self):
37803780
create a twin of Axes for generating a plot with a sharex
37813781
x-axis but independent y axis. The y-axis of self will have
37823782
ticks on left and the returned axes will have ticks on the
3783-
right.
3783+
right. To ensure tick marks of both axis align, see
3784+
:class:`~matplotlib.ticker.LinearLocator`
37843785
37853786
.. note::
37863787
For those who are 'picking' artists while using twinx, pick

lib/matplotlib/ticker.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,10 +1231,11 @@ def view_limits(self, vmin, vmax):
12311231
vmax += 1
12321232

12331233
if rcParams['axes.autolimit_mode'] == 'round_numbers':
1234-
exponent, remainder = _divmod(math.log10(vmax - vmin), 1)
1234+
exponent, remainder = _divmod(math.log10(vmax - vmin),
1235+
math.log10(max([self.numticks-1, 1])))
12351236
if remainder < 0.5:
12361237
exponent -= 1
1237-
scale = 10 ** (-exponent)
1238+
scale = max([self.numticks-1, 1]) ** (-exponent)
12381239
vmin = math.floor(scale * vmin) / scale
12391240
vmax = math.ceil(scale * vmax) / scale
12401241

0 commit comments

Comments
 (0)