gpt4 book ai didi

python - 在 matplotlib 中使用 bbox_inches ='tight' 时,如何获得具有精确大小(以像素为单位)的图像?

转载 作者:行者123 更新时间:2023-12-01 05:48:48 25 4
gpt4 key购买 nike

我想以精确的分辨率(例如 800x600)绘制图表,但使用 bbox_inches='tight' 时,绘图不是全分辨率,而是较小。我可以手动将图像尺寸(以英寸为单位)设置为大约(9.2,6.5),结果为799 x 601,但我希望有更好的解决方案。可以在调整尺寸之前设置bbox_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)

https://img707.imageshack.us/img707/2192/exactsizetest.png

最佳答案

您想使用tight_layout (doc)在调用 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)

关于python - 在 matplotlib 中使用 bbox_inches ='tight' 时,如何获得具有精确大小(以像素为单位)的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15141944/

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