Specify figure size in centimeter in matplotlib

前端 未结 3 1694
没有蜡笔的小新
没有蜡笔的小新 2020-12-05 23:00

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

fi         


        
3条回答
  •  时光说笑
    2020-12-05 23:06

    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.

提交回复
热议问题