Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get matplotlib figure size

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 ?

like image 688
Tristan Avatar asked Apr 17 '15 14:04

Tristan


People also ask

How do you get the size of a figure in Python?

figsize() takes two parameters- width and height (in inches). By default the values for width and height are 6.4 and 4.8 respectively.

How do you determine the size of a plot?

The plotting window size can be found by using dev. size function and we can pass in for inches and cm for centimeters. For example, if we create a plot then we can use dev. size("in") to find the plot size in inches and dev.

What is the unit of figure size in Matplotlib?

The native figure size unit in Matplotlib is inches, deriving from print industry standards. However, users may need to specify their figures in other units like centimeters or pixels.


1 Answers

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 
like image 144
Julien Spronck Avatar answered Sep 23 '22 19:09

Julien Spronck