Repository navigation
Description
As discussed at the matplotlib 2.0 BoF, it would be good to switch the method for default selection of axis limits to something a little less magical. Right now, it does something like: pick the nearest "round number" that is outside of all the data. But this leads to all kinds of weirdnesses. For example:
- If your data goes from 0 to 1, then your limits will be from 0 to 1. Which means that data which actually falls right at 0 or 1 may be obscured by the axis limits -- e.g., here's the
spectral distributionautocorrelation function of white noise (a plot that I've actually used in papers):plt.plot(np.linspace(0, 1, 1000), [1] + [0] * 999). Or if you have a scatter plot, the scatter markers may be half off-screen. A very minimal requirement for a default plotting method is that it should show all the data. - If your data doesn't go between some nice round numbers (which is very common), then you can get very ugly, asymmetric graphs. E.g. because of how the FFT works, it's not uncommon when working with signals to want to e.g. plot a spectrum with 256 elements:
plt.plot(np.random.randn(256)). Notice that this gives x-axis limits of(0, 300)and looks ridiculous.
The easiest way to handle this is to simply ensure that the axis limits are slightly larger than necessary to cover all the data, and then leave it at that. It avoids the really nasty cases, avoids surprises, and if people want something fancier than they almost always have to tweak the limits by hand anyway. As further evidence that this works, this is what R does and it basically just works -- no-one complains.
Specifically, base R calculates default axis limits by: taking the min and max of the data, and then setting the limits 4% further beyond each of those. ggplot2 uses a similar algorithm, but uses 5% by default. (See here, semantics of expand= is (multiplicative expansion, additive expansion).)
This is an issue instead of a PR because I'm not sure how to make this a PR :-). At the BoF someone said that the suggested functionality was already in matplotlib as something called "margins"? But I'm not sure what that is or how to hook it up to rcParams.
Activity
njsmith commentedon Aug 9, 2015
(See also)
tacaswell commentedon Aug 9, 2015
margins already has an rcparam.
njsmith commentedon Aug 9, 2015
Okay, having done a bit of reading, it sounds like: there are rcParams
axes.xmarginandaxes.ymargin, but, they do not actually do what is described in the original post, b/c they are applied in addition to the automagic axis limit finding code, not instead of the automagic axis limit finding code.Specifically,
plt.marginsand friends do what we want if you passtight=True, but the rcParams act as if they callplt.marginswithtight=False(which is #2298).I see two obvious options: either switch the meaning of the existing rcParams so that they imply
tight=Trueas requested in #2298 by the original poster, or else add a second rcParam that orthogonally controls the "autoscaling".efiring commentedon Aug 9, 2015
Although I dislike the ever-increasing number of rcParams, it looks to
me like the simplest and cleanest solution here would be to add one to
control the
tightdefault forautoscale_view. Otherwise,axes.xmarginetc. would need to use None or some other flag toindicate the "classic" default, in place of their present default of
0. I think that might be more confusing than adding, e.g.,
"axes.autoscale_tight" with a new default of True.
On 8/8/15, Nathaniel J. Smith [email protected] wrote:
ellisonbg commentedon Aug 10, 2015
I am in favor of less magic in picking the x and y margins (I think that is
what they are called). I think the approach that ggplot2 uses (5%) is a
good one.
On Sat, Aug 8, 2015 at 9:33 PM, Nathaniel J. Smith <[email protected]
Brian E. Granger
Cal Poly State University, San Luis Obispo
@ellisonbg on Twitter and GitHub
[email protected] and [email protected]
njsmith commentedon Aug 10, 2015
Okay, so looking at axes/_base.py it seems that we currently have the following terminology:
autoscalecan be on or off for each axis independently, and determines whether we adjust the axes at all when new data is plotted.margins can be specified independently for bothxandy, and if present then unconditionally expand the data range by the specified amount when doing autoscalingtightor not, which is confusing -- there's the attributeaxes._tightwhich can be eitherTrueorFalse, and then there's the kwargtight=accepted by theautoscaleandautoscale_viewmethods, which is a tristateTrue/False/None, defaulting toNone.Then when we autoscale, the rules are:
locator.view_limits) if any of the following apply:self._tightis Truetight=Trueself._tightis False,tight=None, and the plot contains something besides an image (!!)I guess what we want for a default is something like: if the plot contains something besides an image, then apply the margins only; otherwise, do nothing.
This means that maintaining strict backwards compatibility is going to be tricky -- potentially people are currently using margins with images, or using margins together with custom locators with custom
view_limitsimplementations. We may just need to have anautoscaleLikeMatplotlib1rcParam?joferkington commentedon Aug 11, 2015
Just as a clarification here, we're only talking about changing what the
axes.xmarginandaxes.ymarginrcParams do, correct?i.e.
axes.xmarginin .maplotlibrc/rcParams is proposed to behave identical toax.margins(x=value)instead ofax.margins(x=value, tight=False)as it currently does?ax.margins(some_percentage)will continue to default totight=True, and autoscaling will continue to default to choosing "even" numbers?If so, great! Of course, the caveats about actually making it work as an rcParam while allowing full backwards compatibility are tough. I do think an rcParam that's equivalent to calling
ax.margins(value)would be a good thing, though.However, I'd argue for against making
ax.margins-style auto-scaling the default.In my experience, "even numbers" are desirable as axes limits about as often as a percentage padding is. In some cases you want one, in some cases the other. Given that it's roughly 50/50 split (though, again, that's just my opinion), it's best to stick with what we have.
ax.margins(percentage)is nice and simple, and the alternative of callingax.autoscale(some_new_parameter)for the other 50% of the time is worse, i.m.o.In my opinion, the main problem is that too few people are aware of
ax.margins(some_percentage)because we don't advertise it enough.I don't think it's worth grossly breaking backwards compatibility for something that's better solved by ensuring people are aware of
ax.margins.Overall, line plots are the most common case where autoscaling with even numbers makes the most sense. (Feel free to argue the contrary there!) They're also probably the most common plot type. I don't think that it's worth breaking that expectation for something that's easy to fix in the cases where you don't want "even-number" autoscaling.
For example, the most frequently asked question I've seen about autoscaling involves bar plots. The default autoscaling is the best choice for
barin general (i.m.o.), asbarcan do lots of things. However, autoscaling often results in visually poor limits in the most common case of a vertical bar plot.This is a classic case where even-number-choosing as limits is not optimal. However, I would argue it's far easier to fix this by letting people know about
ax.marginsfor cases where it makes more sense to use it.For example, with a vertical bar plot, you'll often want a combination of
ax.margins(0.05)andax.set_ylim(bottom=0). Compare the top and bottom plots:However, in my experience, surprisingly few people are aware that it's possible to create the bottom plot without using manually set axes limits.
Overall, I'd argue the bigger problem is that the examples almost never show little things such as this. (I know, I know, PR's welcome!). We can better solve the problem with better advertising of existing functionality instead of breaking backwards compatibility for what's basically a 50/50 advantage/disadvantage.
Just my thoughts on the matter, anyway. I definitely think it's a good discussion to have, regardless!
anntzer commentedon Aug 12, 2015
This is the first time I hear about
plt.margins, which is certainly nice, but handles log-scaled plots incorrectly (cf. #2263, which got closed but not fixed).My 2c. is to give up on round numbers, but fixing this issue is more important.
njsmith commentedon Nov 8, 2015
@joferkington:
The argument against "round number" autoscaling is that sometimes it works fine, and sometimes it fails utterly and produces awful plots (see the examples cited at the top of this thread, which all involve line plots, or your bar plot example). Simple margins OTOH may or may not be the absolute prettiest, but they always work. When choosing defaults IMO the focus shouldn't be on the best cases, but on the worst cases -- if some rule produces 5% better plots 50% of the time and terrible plots the rest of the time, then it's a bad default.
2 remaining items
mdboom commentedon Nov 9, 2015
@joferkington: That's was @tacaswell and I also came to at our "style summit" today, so with good certainty that's what the default will be in 2.0
Scale margins to data, plus padding, by default
Scale margins to data, plus padding, by default
Scale margins to data, plus padding, by default
Scale margins to data, plus padding, by default
Scale margins to data, plus padding, by default
Scale margins to data, plus padding, by default
Scale margins to data, plus padding, by default
Scale margins to data, plus padding, by default
Scale margins to data, plus padding, by default
Scale margins to data, plus padding, by default