【问题标题】:How do to tighten the bounds of my 'matplotlib' figures to be closer to my axes?如何收紧我的“matplotlib”数字的边界以更接近我的轴?
【发布时间】:2014-01-11 00:16:18
【问题描述】:

matplotlib 制作数字时,我发现它“填充”轴周围的空间对我的口味来说太过分了(并且以不对称的方式)。例如与

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

x, y = 12*np.random.rand(2, 1000)
ax.set(xlim=[2,10])
ax.plot(x, y, 'go')

我得到了一些看起来像的东西

(此处以 Adob​​e Illustrator 为例)。

我希望图形的边界更接近四面八方的轴,尤其是左侧和右侧。

如何以编程方式在matplotlib 中相对于每个轴调整这些边界?

【问题讨论】:

  • 您可以使用 add_axes 并手动指定边界,[0,0,1,1] 将完全跨越图形。稍后可以使用 fig.subplot_adjust() 进行调整。

标签: python matplotlib bounds figure axes


【解决方案1】:

尝试:

plt.tight_layout()

默认参数设置为:

plt.tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None)

【讨论】:

  • 这与使用savefig('test.png', bbox_inches='tight')相比如何。
  • @raxacoricofallapatorius - 效果类似,但使用bbox_inches='tight' 会自动选择要保存的图形的子区域,而tight_layout 会更改图形的布局。换句话说,如果你调用plt.show()tight_layout 会改变交互显示的内容,而使用bbox_inches="tight" 的 savefig 不会。
猜你喜欢
  • 2018-10-24
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 2021-12-22
相关资源
最近更新 更多