Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stretching plot horizontally and vertically with matplotlib? [duplicate]

How do I change the size of figure drawn with Matplotlib?

like image 853
tatwright Avatar asked Feb 28 '26 16:02

tatwright


2 Answers

figure tells you the call signature:

from matplotlib.pyplot import figure

figure(figsize=(8, 6), dpi=80)

figure(figsize=(1,1)) would create an inch-by-inch image, which would be 80-by-80 pixels unless you also give a different dpi argument.

like image 80
Jouni K. Seppänen Avatar answered Mar 03 '26 07:03

Jouni K. Seppänen


If you've already got the figure created, you can use figure.set_size_inches to adjust the figure size:

fig = matplotlib.pyplot.gcf()
fig.set_size_inches(18.5, 10.5)
fig.savefig('test2png.png', dpi=100)

To propagate the size change to an existing GUI window, add forward=True:

fig.set_size_inches(18.5, 10.5, forward=True)

Additionally as Erik Shilts mentioned in the comments you can also use figure.set_dpi to "[s]et the resolution of the figure in dots-per-inch"

fig.set_dpi(100)
like image 42
Pete Avatar answered Mar 03 '26 07:03

Pete



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!