Matplotlib Scatter Plot
We can use the scatter() method in pyplot to draw scatter plots.
The syntax of the scatter() method is as follows:
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs)
Parameter Description:
x,y: arrays of the same length, i.e., the data points we are about to plot as a scatter plot, the input data.
s: point size, default 20, can also be an array, where each element is the size of the corresponding point.
c: point color, default blue 'b', can also be a 2D row array of RGB or RGBA.
marker: point style, default small circle 'o'.
cmap: Colormap, default None, a scalar or the name of a colormap, only used when c is an array of floats. If not specified, it is image.cmap.
norm: Normalize, default None, data brightness between 0-1, only used when c is an array of floats.
vmin,vmax:: brightness setting, ignored when the norm parameter is present.
alpha:: transparency setting, between 0-1, default None, i.e., opaque.
linewidths:: the length of the marker point.
edgecolors:: color or sequence of colors, default 'face', possible values are 'face', 'none', None.
plotnonfinite:: boolean, sets whether to use non-finite c (inf, -inf or nan) when drawing points.
****kwargs: **: Other parameters.
In the following example, the scatter() function receives array parameters of the same length, one for the x-axis values and the other for the y-axis values:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
]
)
y
=
np.
array
(
[
1
,
4
,
9
,
16
,
7
,
11
,
23
,
18
]
)
plt.
scatter
(
x
,
y
)
plt.
show
(
)
The display result is as follows:

Set the marker size:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
]
)
y
=
np.
array
(
[
1
,
4
,
9
,
16
,
7
,
11
,
23
,
18
]
)
sizes
=
np.
array
(
[
20
,
50
,
100
,
200
,
500
,
1000
,
60
,
90
]
)
plt.
scatter
(
x
,
y
,
s
=
sizes
)
plt.
show
(
)
The display result is as follows:

Customize point colors:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
]
)
y
=
np.
array
(
[
1
,
4
,
9
,
16
,
7
,
11
,
23
,
18
]
)
colors
=
np.
array
(
[
"red"
,
"green"
,
"black"
,
"orange"
,
"purple"
,
"beige"
,
"cyan"
,
"magenta"
]
)
plt.
scatter
(
x
,
y
,
c
=
colors
)
plt.
show
(
)
The display result is as follows:

Set up two groups of scatter plots:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
5
,
7
,
8
,
7
,
2
,
17
,
2
,
9
,
4
,
11
,
12
,
9
,
6
]
)
y
=
np.
array
(
[
99
,
86
,
87
,
88
,
111
,
86
,
103
,
87
,
94
,
78
,
77
,
85
,
86
]
)
plt.
scatter
(
x
,
y
,
color
=
'hotpink'
)
x
=
np.
array
(
[
2
,
2
,
8
,
1
,
15
,
8
,
12
,
9
,
7
,
3
,
11
,
4
,
7
,
14
,
12
]
)
y
=
np.
array
(
[
100
,
105
,
84
,
105
,
90
,
99
,
90
,
95
,
94
,
100
,
79
,
112
,
91
,
80
,
85
]
)
plt.
scatter
(
x
,
y
,
color
=
'#88c999'
)
plt.
show
(
)
The display result is as follows:

Use random numbers to create a scatter plot:
Example
import
numpy
as
np
import
matplotlib.
pyplot
as
plt
Random number generator seed
np.
random
.
seed
(
19680801
)
N
=
50
x
=
np.
random
.
rand
(
N
)
y
=
np.
random
.
rand
(
N
)
colors
=
np.
random
.
rand
(
N
)
area
=
(
30
- np.
random
.
rand
(
N
)
)
**
2
0 to 15 point radii
plt.
scatter
(
x
,
y
,
s
=
area
,
c
=
colors
,
alpha
=
0.5
)
Set color and transparency
plt.
title
(
"RUNOOB Scatter Test"
)
Set the title
plt.
show
(
)
The display result is as follows:

Colorbar Colormap
The Matplotlib module provides many available colorbars.
A colorbar is like a list of colors, where each color has a value ranging from 0 to 100.
Here is an example of a colorbar:

Setting a colorbar requires the cmap parameter, default 'viridis', and then setting the color values to an array from 0 to 100.
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
5
,
7
,
8
,
7
,
2
,
17
,
2
,
9
,
4
,
11
,
12
,
9
,
6
]
)
y
=
np.
array
(
[
99
,
86
,
87
,
88
,
111
,
86
,
103
,
87
,
94
,
78
,
77
,
85
,
86
]
)
colors
=
np.
array
(
[
0
,
10
,
20
,
30
,
40
,
45
,
50
,
55
,
60
,
70
,
80
,
90
,
100
]
)
plt.
scatter
(
x
,
y
,
c
=
colors
,
cmap
=
'viridis'
)
plt.
show
(
)
The display result is as follows:

To display a colorbar, use the plt.colorbar() method:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
5
,
7
,
8
,
7
,
2
,
17
,
2
,
9
,
4
,
11
,
12
,
9
,
6
]
)
y
=
np.
array
(
[
99
,
86
,
87
,
88
,
111
,
86
,
103
,
87
,
94
,
78
,
77
,
85
,
86
]
)
colors
=
np.
array
(
[
0
,
10
,
20
,
30
,
40
,
45
,
50
,
55
,
60
,
70
,
80
,
90
,
100
]
)
plt.
scatter
(
x
,
y
,
c
=
colors
,
cmap
=
'viridis'
)
plt.
colorbar
(
)
plt.
show
(
)
The display result is as follows:

Try another colorbar parameter, setting cmap to afmhot_r:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
5
,
7
,
8
,
7
,
2
,
17
,
2
,
9
,
4
,
11
,
12
,
9
,
6
]
)
y
=
np.
array
(
[
99
,
86
,
87
,
88
,
111
,
86
,
103
,
87
,
94
,
78
,
77
,
85
,
86
]
)
colors
=
np.
array
(
[
0
,
10
,
20
,
30
,
40
,
45
,
50
,
55
,
60
,
70
,
80
,
90
,
100
]
)
plt.
scatter
(
x
,
y
,
c
=
colors
,
cmap
=
'afmhot_r'
)
plt.
colorbar
(
)
plt.
show
(
)
The display result is as follows:

Colorbar parameter values can be the following:
| Color name | Reserved Keywords | |
|---|---|---|
| Accent | Accent_r | |
| Blues | Blues_r | |
| BrBG | BrBG_r | |
| BuGn | BuGn_r | |
| BuPu | BuPu_r | |
| CMRmap | CMRmap_r | |
| Dark2 | Dark2_r | |
| GnBu | GnBu_r | |
| Greens | Greens_r | |
| Greys | Greys_r | |
| OrRd | OrRd_r | |
| Oranges | Oranges_r | |
| PRGn | PRGn_r | |
| Paired | Paired_r | |
| Pastel1 | Pastel1_r | |
| Pastel2 | Pastel2_r | |
| PiYG | PiYG_r | |
| PuBu | PuBu_r | |
| PuBuGn | PuBuGn_r | |
| PuOr | PuOr_r | |
| PuRd | PuRd_r | |
| Purples | Purples_r | |
| RdBu | RdBu_r | |
| RdGy | RdGy_r | |
| RdPu | RdPu_r | |
| RdYlBu | RdYlBu_r | |
| RdYlGn | RdYlGn_r | |
| Reds | Reds_r | |
| Set1 | Set1_r | |
| Set2 | Set2_r | |
| Set3 | Set3_r | |
| Spectral | Spectral_r | |
| Wistia | Wistia_r | |
| YlGn | YlGn_r | |
| YlGnBu | YlGnBu_r | |
| YlOrBr | YlOrBr_r | |
| YlOrRd | YlOrRd_r | |
| afmhot | afmhot_r | |
| autumn | autumn_r | |
| binary | binary_r | |
| bone | bone_r | |
| brg | brg_r | |
| bwr | bwr_r | |
| cividis | cividis_r | |
| cool | cool_r | |
| coolwarm | coolwarm_r | |
| copper | copper_r | |
| cubehelix | cubehelix_r | |
| flag | flag_r | |
| gist_earth | gist_earth_r | |
| gist_gray | gist_gray_r | |
| gist_heat | gist_heat_r | |
| gist_ncar | gist_ncar_r | |
| gist_rainbow | gist_rainbow_r | |
| gist_stern | gist_stern_r | |
| gist_yarg | gist_yarg_r | |
| gnuplot | gnuplot_r | |
| gnuplot2 | gnuplot2_r | |
| gray | gray_r | |
| hot | hot_r | |
| hsv | hsv_r | |
| inferno | inferno_r | |
| jet | jet_r | |
| magma | magma_r | |
| nipy_spectral | nipy_spectral_r | |
| ocean | ocean_r | |
| pink | pink_r | |
| plasma | plasma_r | |
| prism | prism_r | |
| rainbow | rainbow_r | |
| seismic | seismic_r | |
| spring | spring_r | |
| summer | summer_r | |
| tab10 | tab10_r | |
| tab20 | tab20_r | |
| tab20b | tab20b_r | |
| tab20c | tab20c_r | |
| terrain | terrain_r | |
| twilight | twilight_r | |
| twilight_shifted | twilight_shifted_r | |
| viridis | viridis_r | |
| winter | winter_r |






