当图相对于图的边缘运行时增加边距 [英] Add margin when plots run against the edge of the graph
本文介绍了当图相对于图的边缘运行时增加边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常当我在matplotlib中进行绘图时,会得到如下图:
Often when I plot in matplotlib, I get graphs like this:
您看不到该函数,因为它在图的边缘运行.
You can't see the function because it runs against the edge of the plot.
在这些情况下,是否有任何方法可以自动添加一些余量,使它们看起来像这样:
Is there any way to automatically add some margin in these cases, so that they look like this:
推荐答案
You can use ax.margins()
to set the margins. Example:
In [1]: fig, ax = plt.subplots()
In [2]: ax.plot(np.arange(10), '-o')
Out[2]: [<matplotlib.lines.Line2D at 0x302fb50>]
In [1]: fig, ax = plt.subplots()
In [2]: ax.margins(0.05)
In [3]: ax.plot(np.arange(10), '-o')
Out[3]: [<matplotlib.lines.Line2D at 0x302fb50>]
您还可以仅设置x或y边距.但是,它似乎不是matplotlibrc
选项,因此您可以简单地将其设置为默认行为(因此它不是完全自动的).我打开了 github问题以请求此操作.
You can also set only the x- or the y-margin. However it doesn't seem to be a matplotlibrc
option so that you can simply make this the default behaviour (so it isn't fully automatically). I opened a github issue to request this.
这篇关于当图相对于图的边缘运行时增加边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文