pythonmatplotlibfigsize

Specify figure size in centimeter in 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?


Solution

  • 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.