我正在制作一个嵌入乳胶的出版品质图,我希望在尺寸和字体方面非常精确(因此字体在文章中与图中的尺寸相同).为了防止绘图缩小乳胶,我希望它具有确切的尺寸,但我不能.这是我的代码:
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()
Run Code Online (Sandbox Code Playgroud)
问题在于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=[...])并获得所需的结果,但这是一个手动工作,以使数字正确 - 我不喜欢它.因此,目前我没有看到任何简单而通用的解决方案.
请参阅两个示例re.split()的工作原理:
>>> re.split(',', 'a,b')
['a', 'b']
Run Code Online (Sandbox Code Playgroud)
但
>>> re.split('(,)', 'a,b')
['a', ',', 'b']
Run Code Online (Sandbox Code Playgroud)
为什么我在列表中得到','?怎么避免呢?
我问,因为我想使用类似'xy(a | b)cd'的表达式进行拆分.