Skip to content

ticker.LinearLocator view_limits algorithm improvement closes #6142 #6146

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 1 commit into from
May 4, 2016
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
16 changes: 16 additions & 0 deletions doc/api/api_changes/2015-03-14-SSH.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
``matplotlib.ticker.LinearLocator`` algorithm update
```````````````````````````

The ``matplotlib.ticker.LinearLocator`` is used to define the range and location
of tickmarks of a plot when the user wants a exact number of ticks.
``LinearLocator`` thus differs from the default locator ``MaxNLocator``, which
does not necessarily return the exact user requested number of ticks.

The view range algorithm in ``matplotlib.ticker.LinearLocator`` has been
changed so that more convenient tick location are chosen. The new algorithm
returns a plot view range that is a multiple of the user requested number of
ticks. This ensures tick marks to be located at whole integers more
constistently. For example, when both y-axis of a``twinx`` plot use
``matplotlib.ticker.LinearLocator`` with the same number of ticks, the grids of
both axis will be properly aligned at convenient tick locations.

3 changes: 2 additions & 1 deletion lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3780,7 +3780,8 @@ def twinx(self):
create a twin of Axes for generating a plot with a sharex
x-axis but independent y axis. The y-axis of self will have
ticks on left and the returned axes will have ticks on the
right.
right. To ensure tick marks of both axis align, see
:class:`~matplotlib.ticker.LinearLocator`

.. note::
For those who are 'picking' artists while using twinx, pick
Expand Down
5 changes: 3 additions & 2 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,10 +1231,11 @@ def view_limits(self, vmin, vmax):
vmax += 1

if rcParams['axes.autolimit_mode'] == 'round_numbers':
exponent, remainder = _divmod(math.log10(vmax - vmin), 1)
exponent, remainder = _divmod(math.log10(vmax - vmin),
math.log10(max([self.numticks-1, 1])))
if remainder < 0.5:
exponent -= 1
scale = 10 ** (-exponent)
scale = max([self.numticks-1, 1]) ** (-exponent)
vmin = math.floor(scale * vmin) / scale
vmax = math.ceil(scale * vmax) / scale

Expand Down