AntonK AntonK - 2 months ago 14
Python Question

How to set size for scatter plot

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.

Answer

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.