python - matplotlib 轴 ('tight' )不起作用?

标签 python matplotlib layout plot axes

根据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)

在这些命令中,绘图的限制不会改变:

margins visible

我可能做错了什么?

最佳答案

通过设置 autoscale,您应该可以看到 tight=Truetight=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") 不相关;它仅在文档中说明

‘tight’ Limits set such that all data is shown

确实如此,显示了所有数据(它没有说明将 View 限制准确设置为数据)。

关于python - matplotlib 轴 ('tight' )不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45666740/

相关文章:

Python创建一个包含 float 的可变大小列表

python - 如何使用python获取列表中单个字符串中的值而不用逗号分隔csv文件中的值?

python - 使用 python 连接到 Rest API。如何设置标题和参数?

python - 在 matplotlib 中定义颜色图的中点

android - 如何将 Android 百分比支持库与 ImageView 一起使用?

javascript - HTML-CSS 布局使整个网站居中并卡住菜单

python - 检查 "expanded list"相等性的性能

python - matplotlib 中带有双 for 循环的动画图

python - 如何在 Python 中绘制连续的正弦波?

android - 将 Android View 序列化为 xml 布局