在matplotlib中以厘米为单位指定图形大小 [英] Specify figure size in centimeter in matplotlib
问题描述
我想知道您是否可以在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?
推荐答案
这不是对以下问题的答案:"是否有本机方式?",但我认为有一种更优雅的方式:
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)
然后可以发布plt.figure(figsize=cm2inch(12.8, 9.6))
,我认为这是一种更简洁的方法.该实现还允许我们使用cm2inch((12.8, 9.6))
,我个人不喜欢,但有些人可能会这样做.
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 is no way of doing this natively at the moment, I found a discussion here.
这篇关于在matplotlib中以厘米为单位指定图形大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!