Set Marker Size of Scatter Plot in Matplotlib

  1. s Keyword Argument to Set Matplotlib Scatter Marker Size
  2. Set the Same Scatter Marker Size of All Points in Matplotlib
  3. Increase Scatter Marker Size of Points Non-Uniformly in Matplotlib
  4. markersize Parameter to Set Scatter Marker Size in Matplotlib plot Function

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

s Keyword Argument to Set Matplotlib Scatter Marker Size

The Syntax of the 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 scatter marker size. The default scatter marker size is rcParams['lines.markersize'] ** 2. According to documentation, s is the marker size in points2.

Set the Same Scatter Marker Size of All Points in Matplotlib

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 Scatter Marker Size of Points Non-Uniformly in Matplotlib

Double the Width of Matplotlib Scatter Marker

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 Matplotlib Scatter Marker

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 Set Scatter Marker Size in Matplotlib plot Function

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 the circle is controlled by the markersize parameter.

markersize parameter to control size in plot method

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Matplotlib Scatter

  • Connect Scatterplot Points With Line in Matplotlib