I have three lines setting a plot. I expect the size of the pic.png to be 640x640 pixels. But I got 800x800 picture.
plt.figure(figsize=(8, 8), dpi=80)
plt.scatter(X[:],Y[:])
plt.savefig('pic.png')
BTW I have no problem setting size with object-oriented interface but I need to use pyplot style.
Select Size on the Marks card and adjust the size to make the circles larger. Select Color on the Marks card and set Opacity to 65%. Select Border and pick a medium gray color. Note - the opacity and border is a useful tool when lots of dots plot on top of each other.
By default, Matplotlib uses a resulting of 100 DPI (meaning, per inch). If you were to change this, then the relative sizes that you see would change as well.
The following code produces a 576x576 PNG image in my machine:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10)
y = np.random.rand(10)
plt.figure(figsize=(8, 8), dpi=80)
plt.scatter(x, y)
plt.savefig('pic.png')
Shifting dpi=80
to the plt.savefig
call correctly results in a 640x640 PNG image:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(10)
y = np.random.rand(10)
plt.figure(figsize=(8, 8))
plt.scatter(x, y)
plt.savefig('pic.png', dpi=80)
I can't offer any explanation as to why this happens though.
While Alberto's answer gives you the correct work around, looking at the documentation for plt.savefig
gives you a better idea as to why this behavior happens.
dpi: [ None | scalar > 0 | ‘figure’] The resolution in dots per inch. If None it will default to the value savefig.dpi in the matplotlibrc file. If ‘figure’ it will set the dpi to be the value of the figure.
Short answer: use plt.savefig(..., dpi='figure')
to use the dpi value set at figure creation.
Longer answer: with a plt.figure(figsize=(8,8), dpi=80)
, you can get a 640x640 figure in the following ways:
Pass the dpi='figure'
keyword argument to plt.savefig
:
plt.figure(figsize=(8, 8), dpi=80)
...
plt.savefig('pic.png', dpi='figure')
Explicitly give the dpi you want to savefig
:
plt.figure(figsize=(8, 8))
...
plt.savefig('pic.png', dpi=80)
Edit your matplotlibrc file at the following line:
#figure.dpi : 80 # figure dots per inch
then
plt.figure(figsize=(8, 8))
...
plt.savefig('pic.png') #dpi=None implicitly defaults to rc file value
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