Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Asdiscussed 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 adefault plotting method is that it shouldshow all the data. - If your datadoesn'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 ofexpand=
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.