Specify figure size in centimeter in matplotlib
Asked Answered
I

4

50

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?

Incorporable answered 5/2, 2013 at 13:29 Comment(2)
I have submitted a pull request to the matplotlib repo on GitHub to include set_size_cm and get_size_cm features for figures (github.com/matplotlib/matplotlib/pull/5104) If it is accepted, that should allow you to use a native approach to size-setting in centimeters.Chyou
6 years later the saga continues github.com/matplotlib/matplotlib/issues/12402Huge
P
34

This is not an answer to the 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.


Even though there isn't any way of doing this natively at the moment, I found a discussion here.

Prepared answered 1/4, 2014 at 13:31 Comment(1)
Or def cm2inch(*tupl): return (i/2.54 for i in tupl[0]) if type(tupl[0]) is tuple else (i/2.54 for i in tupl) if you don't mind returning a generator and writing one-linersPulmonic
F
11

I think the solution provided here is also helpful. So, in your case,

cm = 1/2.54  # centimeters in inches
plt.figure(figsize=(12.8*cm, 9.6*cm))
Fasces answered 9/3, 2021 at 1:27 Comment(1)
Dam, beat me too it. This answer should go up as it is the one specified by matplotlib and it is also, by far, the most elegantSphere
M
1

As far as I know, matplotlib doesn't have any conversion functions.

If you often need to convert units, you could consider using pint. It also offers NumPy support.

For your example, you could do something like the following:

from pint import UnitRegistry
ureg = UnitRegistry()

width_cm, height_cm = (12.8 * ureg.centimeter, 9.6 * ureg.centimeter)
width_inch, height_inch = (width_cm.to(ureg.inch), height_cm.to(ureg.inch))

figsize_inch = (width_inch.magnitude, height_inch.magnitude)
fig = plt.figure(figsize=figsize_inch)
Macule answered 29/7, 2014 at 12:2 Comment(0)
A
0

Perhaps you can define your own figure method. It's not very elegant, but it avoids having to specify cm all the time. This can of course also be adapted to other typical pyplot figures like plt.subplots, etc.

def figure(**kwargs):
    """
    create a figure with figsize in cm
    """
    cm = 1/2.56  # convert cm to inch

    kwargs = dict(**kwargs)
    figsize = kwargs.pop("figsize", (15*cm, 10*cm))
    figsize = (figsize[0]*cm, figsize[1]*cm)

    fig = plt.figure(figsize=figsize, **kwargs)
    return fig

figure(figsize=(12.8,9.6))

returns Figure(500x375)

Ariel answered 9/4, 2023 at 21:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.