Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Matplotlib scatter() Function

Matplotlib scatter() Function


Matplotlib Reference Documentation Matplotlib Reference Documentation

scatter() Used to draw scatter plots, where each data point can have its own size, color, and transparency.

Unlike plot(), scatter() allows each point to have different appearance properties, suitable for showing the distribution relationships of three- or four-dimensional data.

Function definition

pyplot interface

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

Axes interface

Axes.scatter(x, y, s=None, c=None, marker='o', cmap=None, norm=None,
    vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None,
    **kwargs)

Parameter Description

Parameters Type Description
x, y array-like The x and y coordinates of the data points (required)
s float or array-like Point size, can be a scalar (same for all points) or an array (different size for each point)
c color or array-like Point color, can be a fixed color or an array of values (mapped to colors via cmap)
marker str Marker shape: 'o' (circle), 's' (square), '^' (up triangle), 'v' (down triangle), 'D' (diamond), '*' (star), '+' (plus), 'x' (cross), etc., default 'o'
cmap str or Colormap Colormap, used when c is an array of numbers, such as 'viridis', 'plasma'
norm Normalize Normalization method for mapping data to colors
vmin, vmax float Data range of the colormap, points outside the range will be clipped
alpha float or array-like Transparency, between 0-1
linewidths float or array-like Point edge line width
edgecolors color or array-like Point edge color, 'face' uses the fill color, 'none' no edge
label str Legend label

The core advantage of scatter() lies in s、c、alpha it can be an array of the same length as x/y, which allows a single scatter plot to display data from 3-4 dimensions at once.


Usage examples

Example 1: Basic scatter plot

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

Generate random data

np.

random

.

seed

(

42

)

x

=

np.

random

.

rand

(

50

)

y

=

np.

random

.

rand

(

50

)

fig

,

ax

=

plt.

subplots

(

layout

=

'constrained'

)

ax.

scatter

(

x

,

y

,

color

=

'steelblue'

,

edgecolors

=

'white'

,

           linewidth

=

0.5

,

s

=

80

)

ax.

set_title

(

'Basic Scatter Plot'

)

ax.

set_xlabel

(

'X'

)

ax.

set_ylabel

(

'Y'

)

ax.

grid

(

True

,

alpha

=

0.3

)

plt.

show

(

)

Example 2: Both size and color vary (displaying multidimensional data)

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

np.

random

.

seed

(

42

)

n

=

100

Four dimensions of data

x

=

np.

random

.

rand

(

n

)

10

         

Dimension 1: x coordinate

y

=

np.

random

.

rand

(

n

)

10

         

Dimension 2: y coordinate

colors

=

np.

random

.

rand

(

n

)

100

   

Dimension 3: color (numeric value)

sizes

=

np.

random

.

rand

(

n

)

300

     

Dimension 4: size

alpha_values

=

np.

random

.

rand

(

n

)

0.7

0.3

 

Dimension 5: transparency

fig

,

ax

=

plt.

subplots

(

figsize

=

(

8

,

6

)

,

layout

=

'constrained'

)

scatter

=

ax.

scatter

(

x

,

y

,

                     c

=

colors

,

         

Color varies with data

                     s

=

sizes

,

           

Size varies with data

                     alpha

=

0.6

,

         

Fixed transparency

                     cmap

=

'viridis'

,

   

Colormap

                     edgecolors

=

'white'

,

                     linewidth

=

0.5

)

Add a color bar

cbar

=

fig.

colorbar

(

scatter

,

ax

=

ax

)

cbar.

set_label

(

'Color Value'

)

ax.

set_title

(

'Multi-dimensional Scatter Plot'

)

ax.

set_xlabel

(

'X Coordinate'

)

ax.

set_ylabel

(

'Y Coordinate'

)

ax.

grid

(

True

,

alpha

=

0.2

)

plt.

show

(

)

Example 3: Comparing multiple groups of scatter points

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

np.

random

.

seed

(

42

)

Generate three sets of data with different distributions

group1_x

=

np.

random

.

normal

(

2

,

0.5

,

100

)

group1_y

=

np.

random

.

normal

(

2

,

0.5

,

100

)

group2_x

=

np.

random

.

normal

(

6

,

0.5

,

100

)

group2_y

=

np.

random

.

normal

(

6

,

0.5

,

100

)

group3_x

=

np.

random

.

normal

(

4

,

1.0

,

100

)

group3_y

=

np.

random

.

normal

(

4

,

1.0

,

100

)

fig

,

ax

=

plt.

subplots

(

figsize

=

(

8

,

6

)

,

layout

=

'constrained'

)

Each group uses a different color and marker

ax.

scatter

(

group1_x

,

group1_y

,

c

=

'#e74c3c'

,

marker

=

'o'

,

           s

=

60

,

label

=

'Group A (tight cluster)'

)

ax.

scatter

(

group2_x

,

group2_y

,

c

=

'#2ecc71'

,

marker

=

's'

,

           s

=

60

,

label

=

'Group B (tight cluster)'

)

ax.

scatter

(

group3_x

,

group3_y

,

c

=

'#3498db'

,

marker

=

'^'

,

           s

=

60

,

label

=

'Group C (spread cluster)'

)

ax.

set_title

(

'Multi-group Scatter Comparison'

)

ax.

set_xlabel

(

'X'

)

ax.

set_ylabel

(

'Y'

)

ax.

legend

(

)

ax.

grid

(

True

,

alpha

=

0.2

)

plt.

show

(

)

Example 4: Scatter plot with a colorbar (bubble chart)

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

Simulated data: population, GDP, and region of different cities

np.

random

.

seed

(

42

)

n

=

30

population

=

np.

random

.

randint

(

50

,

500

,

n

)

   

Population (determines bubble size)

gdp

=

np.

random

.

randint

(

100

,

1000

,

n

)

       

GDP (determines color intensity)

region

=

np.

random

.

choice

(

[

'North'

,

'South'

,

'East'

,

'West'

]

,

n

)

x

=

np.

random

.

rand

(

n

)

100

y

=

np.

random

.

rand

(

n

)

100

fig

,

ax

=

plt.

subplots

(

figsize

=

(

9

,

6

)

,

layout

=

'constrained'

)

scatter

=

ax.

scatter

(

x

,

y

,

                     s

=

population

,

           

Bubble size = population

                     c

=

gdp

,

                 

Bubble color = GDP

                     cmap

=

'YlOrRd'

,

         

Yellow-orange-red colormap

                     alpha

=

0.7

,

                     edgecolors

=

'gray'

,

                     linewidth

=

0.5

)

ax.

set_title

(

'Bubble Chart: City Population vs GDP'

)

ax.

set_xlabel

(

'X Coordinate'

)

ax.

set_ylabel

(

'Y Coordinate'

)

Add a colorbar labeled GDP

cbar

=

fig.

colorbar

(

scatter

,

ax

=

ax

,

label

=

'GDP'

)

ax.

grid

(

True

,

alpha

=

0.2

)

plt.

show

(

)


Common questions

Difference between scatter() and plot()?

plot() Suitable for line plots, where all points share the same color and marker style, with high rendering efficiency.

scatter() Suitable for scatter plots, where each point can have its own size and color, but each point is an independent Artist, which is slower with large datasets.

With many data points (tens of thousands or more), prefer plot() with specified markers to improve performance.

What is the unit of the s parameter?

s It represents the area of the point (in square points, points^2), not the diameter.

For example s=100 and s=400, the latter has 4 times the area and about 2 times the diameter of the former.

Matplotlib Reference Documentation Matplotlib Reference Documentation