当绘图在图形边缘运行时添加边距 [英] Add margin when plots run against the edge of the graph

查看:22
本文介绍了当绘图在图形边缘运行时添加边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常当我在 matplotlib 中绘图时,我会得到这样的图表:

您看不到该函数,因为它运行在图的边缘.

有什么办法可以自动在这些情况下添加一些边距,使它们看起来像这样:

解决方案

您可以使用 ax.margins() 来设置 来请求这个.

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>]

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆