Specify figure size in centimeter in matplotlib

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)