-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Correctly calculate margins on log scales #4592
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
Conversation
delta = (x1 - x0) * self._xmargin | ||
x0 -= delta | ||
x1 += delta | ||
else: # log scale |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please either add another space or remove the comment
Can you add a test for this? I don't think this needs to be an image test, just check that the axes limits are correct after calling |
I've fixed the PEP8 issues and added a test to |
I would be curious to know if this would also work with semilog scales (or On Tue, Jul 7, 2015 at 4:22 AM, David Zaslavsky notifications@github.com
|
Yes, it automatically works with semilog plots. When writing the test case I originally included all four plot types - |
Sorry, not semilog, I got myself confused. I meant symlog. On Tue, Jul 7, 2015 at 3:54 PM, David Zaslavsky notifications@github.com
|
I suspect the ideal way to implement this is via transforms: calculate the margin in axes coordinates, and use the transform to convert to data coordinates. Without something like this, there will be increasing numbers of cases where the result will not be as intended. I'm not sure what difficulties would be encountered in this approach, though. |
When I authored the commit I didn't know there were any other options beyond linear and log scales... so I'm pretty sure I'm not familiar with the capabilities of transforms in matplotlib offhand, but I'll look into it tomorrow. It might still be straightforward. |
Don't forget polar plots! Yeah, I think Eric got the right idea. If you are On Tue, Jul 7, 2015 at 4:06 PM, David Zaslavsky notifications@github.com
|
Working on it. I actually rather like messing with transforms, so I'll go ahead and do it that way - it will just take a bit of care. One other thing: I've noticed that |
I think that data limits band view limits are synonyms in this case, but On Wed, Jul 8, 2015, 3:09 AM David Zaslavsky notifications@github.com
|
After a couple days of looking at this, I think margins for arbitrary projections may just be too complicated to do in general, given the current structure of the code. For a rectangular plot, it's pretty straightforward: compute the view limits without accounting for margins, then transform I think in an ideal world, the most sensible way to implement margins for nearly-arbitrary projections would be setting the initial zoom factor to larger than 1. Projections already have some concept of zooming because it's part of the API, and it seems likely that a projection which doesn't support zooming at all also can't meaningfully be given a margin. However there's currently no general Alternatively, we could implement a margin by inserting a scale factor and translation (equivalent to a rescaling centered on Anyway, those are just some thoughts. I'd be interested to hear responses. I think the issue of margins for a general projection might have to wait, and for now we settle for handling separable projections, as well as polar axes as a special case. For now I have to figure out why the new implementation of |
@diazona Thanks for thinking carefully about this. As something of a side comment, it is not even clear to me that we are handling zoom and pan in polar axes in a useful way; in fact, I don't find it useful at all. What I would want to do with a polar plot is not zoom in on the origin, but zoom in on a feature that could be anywhere; the zooming and panning should be in display space, not r-theta space. The aspect ratio should be preserved, but otherwise it would not differ from zoom in a Cartesian plot. I think this is consistent with your idea of how margins might be implemented as part of a general zoom strategy. |
delta = (x1 - x0) * self._xmargin | ||
x0 -= delta | ||
x1 += delta | ||
if self.get_xscale() == 'linear': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you invert this logic to special case 'log'
instead of 'linear'
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the new implementation the whole if
statement is gone so it's a moot point.
This commit changes the implementation of autoscale_view to use the Axes object's transformations to calculate the margins. This properly deals with nonlinear scales, like log and symlog and any others that may be added in the future. The tests for nonzero margins are also updated accordingly.
Non-separable axes make margins difficult because there's no fully general way of calculating the axis coordinates that should correspond to the new view limits. For now, we leave that part of the method unimplemented and raise a NotImplementedError accordingly.
The latest set of commits implement @efiring's proposal for computing margins using transforms. It works for linear, log, and symlog scales, and supposedly should handle any other separable projection. For nonseparable projections it raises Oh, and I definitely agree that the current handling of zoom in polar plots doesn't make a whole lot of sense. But the changes required to fix zooming and margins for nonseparable projections would be extensive, and probably a matter for a separate pull request. |
Just checking back in on this PR, since it's been "needs_review" for a while: is there anything else that I need to do for it? |
attn @efiring again |
It looks to me like the main idea here has been implemented independently in 41d7e12, #5583: transforms and inverse transforms are being used to apply the margins in axes coordinates. @diazona, I'm sorry your PR languished so long. The comment that Are there projections for which |
Yes, it's a shame it sat around so long, but if the main idea been implemented anyway, I guess this PR has served its purpose. That works for me :-) The thing about the documentation can be handled in a separate bug report. |
This (very small) change calculates the view limits when a log axis is given a nonzero margin, in a way that gives the same visual effect as for a linear axis.