Matplotlib:取消matplotlib 2.0中引入的轴偏移 [英] Matplotlib: cancelling the offset of axis introduced in matplotlib 2.0

查看:88
本文介绍了Matplotlib:取消matplotlib 2.0中引入的轴偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编辑作品时就注意到了这一细微差别.

Just noticed this nuance when I editing my works.

以前,matplotlib如下所示:

Previously, the matplotlib would look like this:

x=[1,2,3,4,5]
y=[4,5,5,2,1]
plot(x,y,'-')

但是我相信在最近升级之后,会有偏移,它会像这样返回

But after recent upgrade I believe, the there are offset, which would return like this

与我现在看到的相比,这有点不必要了.我想知道

It's a little bit unncessary from what I seen now. I want to know

  1. 此偏移量是否是数据可视化的良好做法?如果是这样,我将保持原样.

  1. If this offset is a good practice in data visualization? If so, I'll leave it as it is.

如何取消此偏移量?

我可以通过plt.gca().set_xlim([1, 5])手动恢复限制,但是如果我还有20个图,该比例将无法缩放.我到处搜索,没有找到太多信息.

I can manually restore the limit by plt.gca().set_xlim([1, 5]), but that wouldn't scale if I have another 20 plots. I googled around and didn't find too much info on this.

推荐答案

在matplotlib v2.0.x中,默认轴边距已从0更改为0.05,这是控制轴上数据周围空白的值. .有关此更改背后的原因,请参见此处.

In matplotlib v2.0.x, the default axes margin has changed, from 0 to 0.05, which is the value controlling the whitespace around your data on the axes. See here for more on the reasoning behind this change.

有几种方法可以还原到以前的行为.

There are several ways to revert to the previous behaviour.

1)要将单个Axes实例的margins重置为0:

1) To reset margins to 0 for a single Axes instance:

plt.margins(0)

ax.margins(0)

2)要将脚本中所有图的margins重置为0,请使用rcParams并将其设置在脚本顶部:

2) To reset margins to 0 for all plots in a script, use rcParams and set this at the top of your script:

plt.rcParams['axes.autolimit_mode'] = 'round_numbers'
plt.rcParams['axes.xmargin'] = 0.
plt.rcParams['axes.ymargin'] = 0.

3)要更改计算机上所有图的默认值,请修改matplotlibrc文件以包括以下行:

3) To change the default value for all plots on a machine, modify your the matplotlibrc file to include these lines:

axes.autolimit_mode: round_numbers
axes.xmargin        : 0.
axes.ymargin        : 0.

请注意,要使用方法(1)并真正获得旧的行为,可能还需要设置plt.rcParams['axes.autolimit_mode'] = 'round_numbers'.

Note that to use method (1) and truly get the old behaviour, you may also need to set plt.rcParams['axes.autolimit_mode'] = 'round_numbers'.

这篇关于Matplotlib:取消matplotlib 2.0中引入的轴偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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