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?
From what I understand, matplotlib does not "choose" a suitable aspect ratio per se. Instead, axes automatically fill to the size of the figure. Thus by setting the figure size with a (width, height) tuple you are also setting it's aspect ratio (taking into account the number of subplot axes within the figure as well). Perhaps the axes method set_aspect
will help you? It lets you explicitly set the aspect ratio for an axes object within a figure.
For example, the following will produce a 4"x2" figure but the axes within it will have a 1:1 aspect ratio:
fig, ax = plt.subplots(figsize=(4,2))
ax.set_aspect('equal')
The method set_aspect
can also take a height:width number instead. You could use this to force the axes within to keep a specific aspect ratio regardless of the figure dimensions you choose.
EDIT: This post may also be helpful.
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"]
This returns [6.0, 4.0]
for me.
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"]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With