英文:
Saved matplotlib size on pdf is larger than original figure
问题
当检查Matplotlib图形大小时:
fig_width, fig_height = plt.gcf().get_size_inches()
print(fig_width, fig_height)
结果为:
6.0 4.0
<Figure size 3600x2400 with 0 Axes>
创建图形时的一个参数是:
plt.rcParams["figure.dpi"] = 600
我使用以下代码保存图形:
plt.savefig("figure.pdf", format='pdf', bbox_inches="tight", dpi=600)
检查生成的PDF文件大小时,发现它比原始大小要大:
我还尝试了不指定dpi的方式保存图像,但结果相同:
plt.savefig("figure.pdf", format='pdf', bbox_inches="tight")
我阅读了这个页面,但未找到答案。
请问我应该如何保存Matplotlib图形,以便生成的PDF文件大小为6*4英寸,分辨率为600 dpi?
英文:
When checking matplotlib figure size is :
fig_width, fig_height = plt.gcf().get_size_inches()
print(fig_width, fig_height)
6.0 4.0
´<Figure size 3600x2400 with 0 Axes>´
One of the params to create the figure was:
plt.rcParams["figure.dpi"] = 600
I save the figure with the following code:
plt.savefig("figure.pdf", format= 'pdf', bbox_inches="tight",dpi=600)
When checking resulting pdf size I can see it is larger than the original one:
I also tried saving the image without specifying dpi, but result was the same:
plt.savefig("figure.pdf", format= 'pdf', bbox_inches="tight")
I read this page, but could not find the answer.
How should I save the matploblig figure so that resulting pdf is desired size 6*4 inches and 600 dpi resolution?
答案1
得分: 1
不要使用bbox_inches="tight"
,它尝试去除白色边框,但在这样做时会改变图形的大小。
相反,可以使用layout = 'constrained'
或layout = 'tight'
来创建您的图形:
fig = plt.figure(layout='constrained')
plt.savefig("figure.pdf", dpi=600)
英文:
Do not use bbox_inches="tight"
, which try to remove the white border but changes the fig size while doing so.
Instead, create your figure using layout = 'constrained'
or layout = 'tight'
:
fig = plt.figure(layout='constrained')
plt.savefig("figure.pdf", dpi=600)
答案2
得分: 1
在 plt.savefig()
之前添加以下行解决了问题:
plt.gcf().set_size_inches(6, 4)
英文:
Adding the following line before plt.savefig()
solved the issue:
plt.gcf().set_size_inches(6, 4)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论