How to get matplotlib figure size

PythonMatplotlibSizePixelFigure

Python Problem Overview


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 ?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

Quickly extract figure size in inches

To get width and height in inches I just use:

fig_width, fig_height = plt.gcf().get_size_inches()
print(fig_width, fig_height)

I put this here because this question is the first result that pops up when you search 'get matplotlib figure size', and the api most naturally works in inches, not pixels.

Solution 3 - Python

Maybe this animation might help: enter image description here

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTristanView Question on Stackoverflow
Solution 1 - PythonJulien SpronckView Answer on Stackoverflow
Solution 2 - PythonericView Answer on Stackoverflow
Solution 3 - PythonKolibrilView Answer on Stackoverflow