2017-08-14 63 views
1

按照documentationax.autoscale(tight=True)应该matplotlib axis('tight')不起作用?

如果为True,设置视图限制数据限制;

随着ax.axis('tight')是相似的:

'紧'          限制设置,使得所有的数据都显示

(原文如此)

我们甚至看到它可以在this question的屏幕截图中使用。

但无论我尝试什么,它似乎都不适用于下面的简单示例。这是我键入jupyter-qtconsole

In [27]: f, ax = plt.subplots(1) 

In [28]: ax.plot([0, 1], [1, 0]) 
Out[28]: [<matplotlib.lines.Line2D at 0x825abf0>] 

In [29]: ax.axis('tight') 
Out[29]: (-0.050000000000000003, 1.05, -0.050000000000000003, 1.05) 

In [30]: ax.autoscale(tight=True) 

In [31]: plt.axis('tight') 
Out[31]: (-0.050000000000000003, 1.05, -0.050000000000000003, 1.05) 

In [32]: plt.autoscale(tight=True) 

In [33]: ax.plot([0, 1], [1, 0]) 
Out[33]: [<matplotlib.lines.Line2D at 0x825a4d0>] 

In [34]: ax.autoscale(enable=True, axis='x', tight=True) 

纵观这些命令,该地块的限制不会改变:

margins visible

什么可能我是做错了什么?

回答

1

你不一定做错了什么。您正在使用matplotlib版本2(或更高版本)。在这个版本中,默认的绘图布局被改变了,所以轴的两端都加了5%的填充。这里的描述的情节布局的链接:https://matplotlib.org/users/dflt_style_changes.html#plot-layout

从链接,将其改回为“经典”的风格,使用方法:

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

工作。我将'mpl.rcParams ['axes.autolimit_mode']'留在了'data'的默认值,这符合数据极限,而不是下一个勾号(即'round_numbers'行为)。另外,我想说的是,它与一般常识略有不同,特别要求'axis('tight')'不会折叠这些边距,这似乎可以在每个plot的基础上进行(https: //stackoverflow.com/a/41748745/1143274)。 –

1

通过设置autoscale你应该看到tight=True之间所需的区别tight=False

f, (ax, ax2) = plt.subplots(ncols=2) 

ax.plot([0, 1], [1, 0], label="tight=True") 
ax.autoscale(enable=True, axis='both', tight=True) 

ax2.plot([0, 1], [1, 0], label="tight=False") 
ax2.autoscale(enable=True, axis='both', tight=False) 

ax.legend() 
ax2.legend() 

enter image description here

你可能注意到ax.axis("tight")是没有关系的;它只有在文档中指出

“紧”的限制设置,使得所有的数据显示

这的确是这样的,所有的数据显示(它没有说关于设置什么查看对数据的限制)。

+0

现在这很有趣,因为当我复制这个代码时,我得到了两张图,都看起来像你的右手边情节。然而,从线的颜色,我推断你也使用Matplotlib 2+(除非你已经改变了Matplotlib 1.3的颜色方案?)你的'matplotlib.rcParams ['axes.xmargin']的值是多少? ? –

+1

我正在使用matplotlib 2.0.2,边距是默认的'print matplotlib.rcParams ['axes.xmargin']#results in 0.05'。 – ImportanceOfBeingErnest

+0

解决了!自3月份以来,我没有更新Python库; 'matplotlib .__ version__'是''2.0.0b3''。从那时起它一定是固定的。 –