Skip to content
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

matplotlib.ticker.LinearLocator view_limits algorithm improvement? #6142

Closed
maqifrnswa opened this issue Mar 11, 2016 · 3 comments
Closed

matplotlib.ticker.LinearLocator view_limits algorithm improvement? #6142

maqifrnswa opened this issue Mar 11, 2016 · 3 comments
Milestone

Comments

@maqifrnswa
Copy link
Contributor

Inspecting the code of matplotlib.ticker.LinearLocator

class LinearLocator(Locator):

you can see that the view limits are chosen such that the difference between vmin and vmax is an interger multiple of scale, which is itself a power of 10. Therefore, the range will be nicely divisible when divided by 10 (11 tickmarks).

def view_limits(self, vmin, vmax):

Therefore, the view_limits function determines the limits assuming there are 11 ticks. This assumption is implicit in two lines:

exponent, remainder = divmod(math.log10(vmax - vmin), 1)

scale = 10 ** (-exponent)

Code is repeated here:

exponent, remainder = divmod(math.log10(vmax - vmin), 1)
if remainder < 0.5:
    exponent -= 1
scale = 10 ** (-exponent)
vmin = math.floor(scale * vmin) / scale
vmax = math.ceil(scale * vmax) / scale

Since we know the number of ticks, from self.num_ticks, we can generalize the current algorithm to be better suited for any number of ticks. Suggested generalized algorithm:

exponent, remainder = divmod(math.log10(vmax - vmin), math.log10(self.num_ticks-1))
if remainder < 0.5:
    exponent -= 1
scale = (self.num_ticks-1) ** (-exponent)
vmin = math.floor(scale * vmin) / scale
vmax = math.ceil(scale * vmax) / scale

This generalized expression reduces to the current form when self.num_ticks==11 (which is the current default). For other cases, here is an example:
when num_ticks = 10, vmin = 20, vmax=90
Current algorithm returns vmin = 20, vmax=90, corresponding to ticks spaced by 7.77778.

The proposed algorithm returns vmin = 18, vmax = 90, corresponding to ticks spaced by 8.

Is this something worth doing? The patch is trivial -- just changing two lines of code. I can turn this in to a pull request to illustrate if it is helpful.

@tacaswell
Copy link
Member

A pull request would help to clairify (and will allow the tests to run).

@tacaswell tacaswell added this to the 2.0 (style change major release) milestone Mar 11, 2016
@tacaswell
Copy link
Member

provisionally tagging an 2.0 in case we like this change and it breaks tests, can be pushed out if needed.

maqifrnswa added a commit to maqifrnswa/matplotlib that referenced this issue Mar 11, 2016
@maqifrnswa
Copy link
Contributor Author

No problem, here's a pull request:
#6146

@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.0 (style change major release) Mar 14, 2016
maqifrnswa added a commit to maqifrnswa/matplotlib that referenced this issue Mar 29, 2016
tacaswell added a commit that referenced this issue May 4, 2016
API: ticker.LinearLocator view_limits algorithm changes

closes #6142
@QuLogic QuLogic modified the milestones: 2.0 (style change major release), 2.1 (next point release) May 12, 2016
tacaswell added a commit that referenced this issue May 13, 2016
API: ticker.LinearLocator view_limits algorithm changes

closes #6142
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants