How to set marker size of scatter plot in Matplotlib

  1. s keyword argument of the function scatter()
  2. markersize parameter to control size in plot method

The size of markers in the scatter plot is controlled by s keyword argument of the function scatter(), where s is a scalar or an array.

s keyword argument of the function scatter()

Syntax for scatter function:

matplotlib.pyplot.scatter(x, y,
                          s=None,
                          c='b', 
                          marker='o', 
                          cmap=None, 
                          norm=None,
                          vmin=None, 
                          vmax=None, 
                          alpha=None, 
                          linewidths=None,
                          faceted=True, 
                          verts=None,
                          hold=None, 
                          **kwargs)

where, s is a scalar or an array of the same length as x and y, to set the markersize. The default marker size is rcParams['lines.markersize'] ** 2. According to documentation, s is the marker size in points**2.

Set the same marker size of all points

import numpy as np
import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y=np.sin(x)

plt.scatter(x,y,s=500,c='magenta')

plt.title('Scatter plot of sinx')
plt.xlabel('x')
plt.ylabel('sinx')
plt.xlim(0,6)
plt.ylim(-2,2)
plt.show()

Increased markersize of scatter plot uniformly

Increase markersize of points non-uniformly

double the width of markers

To double the width (or height) of the marker we need to increase s by a factor of 4 as A = W*H => (2W)*(2H)= 4A.

import matplotlib.pyplot as plt
x = [1,2,3,4,5]
y = [a**2 for a in x]
s = [10*4**n for n in range(len(x))]
plt.scatter(x,y,s=s)
plt.title('Doubling width of marker in scatter plot')
plt.xlabel('x')
plt.ylabel('x**2')
plt.xlim(0,6)
plt.ylim(0,30)
plt.show()

doubled width of markers

double the area of markers

To double the area of the marker, we increase the area by a factor of 2 so the marker size scales linearly with the area.

import matplotlib.pyplot as plt
x = [1,2,3,4,5]
y = [a**2 for a in x]
s = [10*2**n for n in range(len(x))]
plt.scatter(x,y,s=s)
plt.title('Doubling area of marker in scatter plot')
plt.xlabel('x')
plt.ylabel('x**2')
plt.xlim(0,6)
plt.ylim(0,30)
plt.show()

doubled area of markers

markersize parameter to control size in plot method

import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y = [0]*len(x)

plt.plot(x,y, 'bo', markersize=10)  
plt.show()

Here the area of circle is controlled by markersize parameter.

markersize parameter to control size in plot method

Related Article - Matplotlib Scatter

  • How to connect scatterplot points with line in Matplotlib
  • How to Plot list of x,y coordinates in Matplotlib
  • comments powered by Disqus