Matplotlib Scatter Plot
We can use the scatter()
method from pyplot to create scatter plots.
The syntax for 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 Descriptions:
x, y: Arrays of the same length, representing the data points to be plotted, input data.
s: Size of the points, default is 20. It can also be an array where each element corresponds to the size of a point.
c: Color of the points, default is blue 'b'. It can also be an RGB or RGBA two-dimensional row array.
marker: Style of the points, default is a small circle 'o'.
cmap: Colormap, default is None. It can be a scalar or the name of a colormap. It is used only when c
is an array of floating-point numbers. If not specified, it defaults to image.cmap
.
norm: Normalize, default is None. It scales data to the range 0-1, used only when c
is an array of floating-point numbers.
vmin, vmax: Brightness settings, ignored if norm
is present.
alpha: Transparency setting, between 0 and 1, default is None, meaning opaque.
linewidths: Width of the marker lines.
edgecolors: Color or sequence of colors, default is 'face'. Possible values are 'face', 'none', or None.
plotnonfinite: Boolean, sets whether to plot points with non-finite c
(inf, -inf, or nan).
**kwargs: Other parameters.
The following example demonstrates the scatter()
function receiving arrays of the same length, one for the x-axis values and another 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 result is displayed as follows:
To set the icon 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 result is displayed as follows:
To customize the color of the points:
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 result is displayed as follows:
To set two sets 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 result is displayed as follows:
To use random numbers to set the 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("tutorialpro Scatter Test") # Set title
plt.show()
The result is displayed as follows:
Colorbar Colormap
The Matplotlib module provides many available colormaps.
A colormap is like a list of colors where each color has a value ranging from 0 to 100.
Here is an example of a colormap:
To set the colormap, use the cmap
parameter, with the default value being 'viridis', and then set 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 result is displayed as follows:
To display the 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 result is displayed as follows:
To change the colormap parameter, set 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 result is displayed as follows:
The colormap parameter values can be the following:
Color Name | Reversed Color Name | |
---|---|---|
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 |