matplotlib:在不更改纵横比的情况下设置图形的宽度或高度 [英] matplotlib: Set width or height of figure without changing aspect ratio
问题描述
我想选择图形的宽度,同时仍然让matplotlib选择合适的纵横比.我知道的每一种改变图形大小的方法都需要一个 (width, height) 元组,它强制一定的纵横比.有什么方法可以仅指定宽度(或仅高度)并允许matplotlib选择合适的宽高比?
I'd like to choose the width of a figure, while still letting matplotlib choose the aspect ratio that it finds suitable. Every method I know to change the figure size requires a (width, height) tuple, which forces a certain aspect ratio. Is there any way to specify just the width (or just the height) and allow matplotlib to choose a suitable aspect ratio?
推荐答案
我了解到matplotlib实际上只是对所有图形使用恒定的默认图形大小.此值存储在 rcParams
中,并且可以使用
I've learned that matplotlib actually just uses a constant default figure size for all figures. This value is stored in the rcParams
, and can be viewed with
plt.rcParams["figure.figsize"]
这会为我返回 [6.0,4.0]
.
如果您想保持相同的宽高比(即纵横比"),例如,您可以将其加倍:
If you want to keep the same width to height ratio (ie "aspect ratio"), you can, for example, double it with:
plt.rcParams["figure.figsize"] = [2 * i for i in plt.rcParams["figure.figsize"]]
这篇关于matplotlib:在不更改纵横比的情况下设置图形的宽度或高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!