pad_inches=0 和 bbox_inches=“tight";使图小于声明的figsize [英] pad_inches=0 and bbox_inches="tight" makes the plot smaller than declared figsize

查看:239
本文介绍了pad_inches=0 和 bbox_inches=“tight";使图小于声明的figsize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am producing a publication-quality plot to be embedded in latex and I would like to be very precise in terms of sizes and fonts (so that fonts are of the same size in the article as in the plot). To prevent the plot from scaling in latex I would like to have it exact size, but I cannot. Here is my code:

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英寸.但是我希望他们节省空间.那么如何解决呢?

The problem is with bbox_inches='tight' and pad_inches=0. Adding those options makes my plot 4.76 inches wide instead of declared 5 inches. But I want them to save space. So how to solve it?

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

Well, the answers suggest to remove bbox_inches='tight' and pad_inches=0 and use just the tight_layout(). Then the images is of right size, however it has still some white padding around. I can remove it with fig.tight_layout(pad=0), but then the figure title it moved inside the box, which looks ugly. On the other hand I can use tight_layout(rect=[...]) and obtain the desired result, but it is a manual work to get the numbers right - I don't like it. Thus, currently I don't see any easy and general solution to my problem.

推荐答案

您遇到的问题是 bbox_inches ='tight'只是删除了图形周围的所有多余空白,它确实在渲染之后,实际上不会重新排列图中的任何内容.

The problem you are having is that bbox_inches='tight' just removes all of the extra white space around your figure, it does not actually re-arrange anything in your figure, after it has been rendered.

您可能需要调整传递给 tight_layout 的参数(教程)以达到您想要的效果.

You might need to tweak the parameters you pass to tight_layout (tutorial) to get your desired effect.

希望这能让您指明正确的方向.

Hopefully this gets you pointed in the right direction.

这篇关于pad_inches=0 和 bbox_inches=“tight";使图小于声明的figsize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆