Python 如何获取matplotlib图形大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29702424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:54:07  来源:igfitidea点击:

How to get matplotlib figure size

pythonmatplotlibsizepixelfigure

提问by Tristan

For a project, I need to know the current size (in pixels) of my matplotlib figure, but I can't find how to do this. Does anyone know how to do this ?

对于一个项目,我需要知道我的 matplotlib 图的当前大小(以像素为单位),但我找不到如何做到这一点。有谁知道如何做到这一点 ?

采纳答案by Julien Spronck

import matplotlib.plt
fig = plt.figure()
size = fig.get_size_inches()*fig.dpi # size in pixels

To do it for the current figure,

要为当前图形执行此操作,

fig = plt.gcf()
size = fig.get_size_inches()*fig.dpi # size in pixels

You can get the same info by doing:

您可以通过执行以下操作获得相同的信息:

bbox = fig.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
width, height = bbox.width*fig.dpi, bbox.height*fig.dpi