根据documentation , ax.autoscale(tight=True)
应该
If True, set view limits to data limits;
与 ax.axis('tight')
相似:
‘tight’ Limits set such that all data is shown
(原文如此)
我们甚至在 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)
在这些命令中,绘图的限制不会改变:
我可能做错了什么?
最佳答案
通过设置 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()
您可能会注意到 ax.axis("tight")
不相关;它仅在文档中说明
‘tight’ Limits set such that all data is shown
确实如此,显示了所有数据(它没有说明将 View 限制准确设置为数据)。
关于python - matplotlib 轴 ('tight' )不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45666740/