Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify figure size in centimeter in matplotlib

Tags:

matplotlib

I am wondering whether you can specify the size of a figure in matplotlib in centimeter. At the moment I write:

def cm2inch(value):     return value/2.54  fig = plt.figure(figsize=(cm2inch(12.8), cm2inch(9.6))) 

But is there a native approach?

like image 407
HyperCube Avatar asked Feb 05 '13 13:02

HyperCube


People also ask

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.

What is the default figure size in Matplotlib?

If not provided, defaults to rcParams["figure. figsize"] (default: [6.4, 4.8]) = [6.4, 4.8] .


2 Answers

This is not an answer to a question ''Is there a native way?'', but I think, that there is a more elegant way:

def cm2inch(*tupl):     inch = 2.54     if isinstance(tupl[0], tuple):         return tuple(i/inch for i in tupl[0])     else:         return tuple(i/inch for i in tupl) 

Then one can issue plt.figure(figsize=cm2inch(12.8, 9.6)), which I think is a much cleaner way. The implementation also allows us to use cm2inch((12.8, 9.6)), which I personally do not prefer, but some people may do.


EDIT: Even though there is no way of doing this natively at the moment, I found a discussion here.

like image 121
aignas Avatar answered Oct 01 '22 20:10

aignas


I have submitted a pull request to the matplotlib repo on GitHub to include set_size_cm and get_size_cm features for figures (https://github.com/matplotlib/matplotlib/pull/5104)

If it is accepted, that should allow you to use a native approach to size-setting in centimeters.

like image 40
Sameer Puri Avatar answered Oct 01 '22 20:10

Sameer Puri