Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 437
AntonK Avatar asked Sep 26 '16 21:09

AntonK


People also ask

How do you increase the size of a circle in a scatter plot?

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.

What is default marker size in Matplotlib?

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.


2 Answers

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.

like image 100
A. Garcia-Raboso Avatar answered Sep 28 '22 04:09

A. Garcia-Raboso


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:

  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
    

    then

    plt.figure(figsize=(8, 8))
    ...
    plt.savefig('pic.png') #dpi=None implicitly defaults to rc file value
    
like image 43
wflynny Avatar answered Sep 28 '22 04:09

wflynny