close

How to set size for scatter plot

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to set size for scatter plot in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to set size for scatter plot?

  1. How to set size for scatter plot?

    The following code produces a 576×576 PNG image in my machine:
    import numpy as np import matplotlib.pyplot as plt

  2. set size for scatter plot

    The following code produces a 576×576 PNG image in my machine:
    import numpy as np import matplotlib.pyplot as plt

Method 1

The following code produces a 576×576 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 640×640 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.

Method 1

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 640×640 figure in the following ways:

  1. Pass the dpi='figure' keyword argument to plt.savefig:plt.figure(figsize=(8, 8), dpi=80) ... plt.savefig('pic.png', dpi='figure')
  2. Explicitly give the dpi you want to savefig:plt.figure(figsize=(8, 8)) ... plt.savefig('pic.png', dpi=80)
  3. Edit your matplotlibrc file at the following line:#figure.dpi : 80 # figure dots per inch thenplt.figure(figsize=(8, 8)) ... plt.savefig('pic.png') #dpi=None implicitly defaults to rc file value

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read