在图上设置自动缩放限制以在所有点周围具有缓冲 [英] Set autoscale limits on plot to have buffer around all points
问题描述
我想在matplotlib中使用pyplot绘制一组点,但没有一个点在轴的边缘.自动缩放(或其他方式)将xlim
和ylim
设置为通常使第一个点和最后一个点位于x = xmin
或xmax
上,从而在某些情况下难以读取.
I would like to plot a set of points using pyplot in matplotlib but have none of the points be on the edge of my axes. The autoscale (or something) sets the xlim
and ylim
such that often the first and last points lie at x = xmin
or xmax
making it difficult to read in some situations.
这对于loglog()
或semilog()
绘图经常会出现问题,因为自动缩放会希望xmin
和xmax
为十的幂,但是如果我的数据仅包含三个点,例如在xdata = [10**2,10**3,10**4]
处,第一个和最后一个点将位于图的边界上.
This is more often problematic with loglog()
or semilog()
plots because the autoscale would like xmin
and xmax
to be exact powers of ten, but if my data contains only three points, e.g. at xdata = [10**2,10**3,10**4]
then the first and last points will lie on the border of the plot.
这是我的解决方案,将10%的缓冲区添加到图形的任一侧.但是,有什么方法可以更优雅或更自动地做到这一点吗?
This is my solution to add a 10% buffer to either side of the graph. But is there a way to do this more elegantly or automatically?
from numpy import array, log10
from matplotlib.pyplot import *
xdata = array([10**2,10**3,10**4])
ydata = xdata**2
figure()
loglog(xdata,ydata,'.')
xmin,xmax = xlim()
xbuff = 0.1*log10(xmax/xmin)
xlim(xmin*10**(-xbuff),xmax*10**(xbuff))
我希望可以在每次绘制这样的图时都可以轻松使用的一线或两线解决方案.
I am hoping for a one- or two-line solution that I can easily use whenever I make a plot like this.
要弄清楚我在解决方法中正在做什么,我应该在线性空间(而不是日志空间)中添加一个示例:
To make clear what I'm doing in my workaround, I should add an example in linear space (instead of log space):
plot(xdata,ydata)
xmin,xmax = xlim()
xbuff = 0.1*(xmax-xmin)
xlim(xmin-xbuff,xmax+xbuff))
与前面的示例相同,但是线性轴.
which is identical to the previous example but for a linear axis.
一个相关的问题是有时限制太大.假设我的数据类似于ydata = xdata**0.25
,因此范围内的差异远小于十年,但恰好以10**1
结尾.然后,自动缩放ylim
为10**0
至10**1
,尽管数据仅位于图的顶部.使用上面的解决方法,我可以增加ymax
以便使第三点完全在限制范围内,但是我不知道如何增加 ymin
以便下部的空白较少我的情节也就是说,我要说的并不是我总是想把限制分散开来,而是想在我所有的点上都有一些恒定(或成比例的)缓冲.
A related problem is that sometimes the limits are too large. Say my data is something like ydata = xdata**0.25
so that the variance in the range is much less than a decade but ends at exactly 10**1
. Then, the autoscale ylim
are 10**0
to 10**1
though the data are only in the top portion of the plot. Using my workaround above, I can increase ymax
so that the third point is fully within the limits but I don't know how to increase ymin
so that there is less whitespace at the lower portion of my plot. i.e., the point is that I don't always want to spread my limits apart but would just like to have some constant (or proportional) buffer around all my points.
推荐答案
@askewchan通过编辑matplotlibrc配置文件并直接从终端运行python,我成功地实现了如何更改matplotlib设置. 尚不清楚原因,但是当我从spyder3(我的IDE)运行python时,matplotlibrc无法正常工作.请按照此处的步骤
@askewchan I just succesfully achieved how to change matplotlib settings by editing matplotlibrc configuration file and running python directly from terminal. Don't know the reason yet, but matplotlibrc is not working when I run python from spyder3 (my IDE). Just follow steps here matplotlib.org/users/customizing.html.
1)解决方案一(所有图的默认设置)
尝试将其放入matplotlibrc中,您将看到缓冲区增加:
Try put this in matplotlibrc and you will see the buffer increase:
axes.xmargin : 0.1 # x margin. See `axes.Axes.margins`
axes.ymargin : 0.1 # y margin See `axes.Axes.margins`
值必须在0到1之间.
不好.:由于存在错误,秤无法正常工作.它会在matplotlib 1.5中修复(我的版本是1.4.3 ...).更多信息:
Obs.: Due to bugs, scale is not correctly working yet. It'll be fixed for matplotlib 1.5 (mine is 1.4.3 yet...). More info:
- axes.xmargin/ymargin rcParam behaves differently than pyplot.margins() #2298
- Better auto-selection of axis limits #4891
2)解决方案二(分别针对代码中的每个图)
还有margins函数(直接在代码中放置).示例:
There is also the margins function (for put directly in the code). Example:
import numpy as np
from matplotlib import pyplot as plt
t = np.linspace(-6,6,1000)
plt.plot(t,np.sin(t))
plt.margins(x=0.1, y=0.1)
plt.savefig('plot.png')
Obs..在这里,缩放有效(在x范围和y范围之前和之后,0.1将增加缓冲区的10%).
Obs.: Here scale is working (0.1 will increase 10% of buffer before and after x-range and y-range).
这篇关于在图上设置自动缩放限制以在所有点周围具有缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!