Python 当使用bbox_inches='时,如何获得精确大小(以像素为单位)的图像;紧密的';在matplotlib中?

Python 当使用bbox_inches='时,如何获得精确大小(以像素为单位)的图像;紧密的';在matplotlib中?,python,matplotlib,Python,Matplotlib,我想以精确的分辨率(例如800x600)绘制图形,但当使用bbox\u inches='tight”时,图形不是以完全分辨率绘制的,而是更小。 我可以手动将图像大小(以英寸为单位)设置为大约(9.2,6.5),结果是799 x 601,但我希望有更好的解决方案。在调整尺寸之前,您可以设置bbox\u inches='tight' import matplotlib as mlp import matplotlib.pyplot as plt import numpy as np fig = p

我想以精确的分辨率(例如800x600)绘制图形,但当使用
bbox\u inches='tight
”时,图形不是以完全分辨率绘制的,而是更小。 我可以手动将图像大小(以英寸为单位)设置为大约(9.2,6.5),结果是799 x 601,但我希望有更好的解决方案。在调整尺寸之前,您可以设置
bbox\u inches='tight'

import matplotlib as mlp
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
x = y = np.arange(0, 1, 0.1)
plt.plot(x, y, label='my function')
plt.title('title')
ax.set_xlabel('xAxis')
ax.set_ylabel('yAxis')

#print fig.get_size_inches()
#fig.set_size_inches(9.2, 6.5)

plt.savefig('exact_size_test.png', bbox_inches='tight', dpi=100)

在调用
savefig

import matplotlib as mlp
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
x = y = np.arange(0, 1, 0.1)
plt.plot(x, y, label='my function')
plt.title('title')
ax.set_xlabel('xAxis')
ax.set_ylabel('yAxis')

#print fig.get_size_inches()
fig.set_size_inches(8, 6, forward=True)
fig.tight_layout()
plt.savefig('exact_size_test.png', dpi=100)

再次感谢您的帮助,这是我一直在寻找的,但我有一个问题,当图例在绘图
plt.legend(bbox\u to\u anchor=(1,0.5),loc='center left')
我知道我可以通过添加这个参数
bbox\u extra\u artists=(图.get\u axies()[0].get\u legend(),)
到plt.savefig()来解决这个问题。但是如果我在宣布额外的艺术家之前采用紧凑的布局,传奇就会被打断。我没有找到在savefig之外设置bbox_额外艺术家的任何选项。我知道我可以缩小绘图宽度以适应图例,但我希望尽可能保持它的动态性。您使用的是什么版本的MPL?这是我认为最近已经解决的问题(紧凑布局仍然是新的),我使用的是MPL版本1.2.0。下面是一个示例:将后端从“TkAgg”更改为“Agg”没有任何效果。您解决了这个问题吗?是的,“fig\uu tight\u layout()”效果很好,所以我最终接受了它(迟做总比不做强:)