gpt4 book ai didi

matplotlib - pad_inches=0 和 bbox_inches ="tight"使绘图小于声明的 figsize

转载 作者:行者123 更新时间:2023-12-03 20:53:05 26 4
gpt4 key购买 nike

我正在制作一个嵌入 latex 中的出版物质量图,我希望在大小和字体方面非常精确(以便文章中的字体与图中的字体大小相同)。为了防止绘图在 latex 中缩放,我想要它的确切尺寸,但我不能。这是我的代码:

import matplotlib.pyplot as plt
from matplotlib import rc, rcParams
from numpy import sin

rc('text', usetex=True)
rc('font', family='serif', serif='Computer Modern Roman', size=8)
rc('legend', fontsize=10)

width_in = 5
fig = plt.figure(1, figsize=(width_in, 2))
ax = fig.add_subplot(111)
ax.plot(range(0,100), sin(range(0,100)))

fig.tight_layout()
fig.savefig('test.eps', bbox_inches='tight', pad_inches=0)
plt.close()

问题在于 bbox_inches='tight' 和 pad_inches=0。添加这些选项使我的绘图宽度为 4.76 英寸,而不是声明的 5 英寸。但我希望它们节省空间。那么如何解决呢?

编辑:嗯,答案建议删除 bbox_inches='tight'pad_inches=0并仅使用 tight_layout() .然后图像大小合适,但是它周围仍然有一些白色填充。能用 fig.tight_layout(pad=0)去掉,但随后它移动到框内的图形标题,看起来很难看。另一方面,我可以使用 tight_layout(rect=[...])并获得所需的结果,但要获得正确的数字是一项手动工作 - 我不喜欢它。因此,目前我没有看到任何简单和通用的解决方案来解决我的问题。

最佳答案

您遇到的问题是 bbox_inches='tight'只是删除了图形周围的所有额外空白,在渲染图形后,它实际上并没有重新排列图形中的任何内容。

您可能需要调整传递给 tight_layout 的参数( tutorial ) 以获得您想要的效果。

希望这能让你指向正确的方向。

关于matplotlib - pad_inches=0 和 bbox_inches ="tight"使绘图小于声明的 figsize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62032636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com