In This Article, You Will Learn About Python Matplotlib Scatter.
Python Matplotlib Scatter – Before moving ahead, let’s take a look at Python Matplotlib Subplots
Table of Contents
Create Scatter Plots
To draw scatter graph, we can use scatter() function with Pyplot.
Its scatter() function plots one dot for every observation. It requires two arrays of equal length for one for the values on the x-axis and another for values on the y-axis.
Example: Plot a scatter graph.
import matplotlib.pyplot as plt
import numpy as np
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
plt.scatter(x_axis, y_axis)
plt.show()
Compare Plots or Multiple Scatter Plot
Example: Draw two scatter graphs at the exact figure.
import matplotlib.pyplot as plt
import numpy as np
#plot 1
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
plt.scatter(x_axis, y_axis)
#plot 2
x = np.array([10, 18, 25, 36, 48])
y = np.array([5, 21, 15, 26, 29])
plt.scatter(x, y)
plt.show()
Notice: The two plots are plotted using two colors. By default, orange and blue, you’ll learn to switch colors later in this lesson.
Colors
We can pass argument color or c in the scatter() function to set color for each scatter plot.
Example: Set the color to the scatter point.
import matplotlib.pyplot as plt
import numpy as np
#plot 1
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
plt.scatter(x_axis, y_axis, color = 'green')
#plot 2
x = np.array([10, 18, 25, 36, 48])
y = np.array([5, 21, 15, 26, 29])
plt.scatter(x, y, color = '#90EE90')
plt.show()
Color Each Dot
Bypassing an array of colors, we can set each dot’s color in the scatter graph as a value of argument color or c.
Note: You cannot use color argument for this; you can only use argument c.
Example: Set any color to each dot of the scatter graph.
import matplotlib.pyplot as plt
import numpy as np
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
colors = np.array(['green', 'red', 'brown', 'yellow', 'gray'])
plt.scatter(x_axis, y_axis, c = colors)
plt.show()
ColorMap
The Matplotlib provides several available colormaps.
A colormap is a list of colors, where each color has a value that ranges from 0 to 100.
In Matplotlib, the colormap is said ‘virdis’ and it ranges from 0, which is a purple color, and up to 100, which is a yellow color.
Use the ColorMap
To use colormap, we can pass argument cmap with the value of colormap. In this case ‘Dark2‘ is one of the built-in colormaps available in Matplotlib.
Additionally, you need to build an array that contains values (from 0 to 100) each for each point in the scatter graph.
Example: Pass an array with random value as a value of colormap to set color to scatter graph.
import matplotlib.pyplot as plt
import numpy as np
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
colors = np.array([0, 10, 25, 38, 45])
plt.scatter(x_axis, y_axis, cmap = 'Dark2')
plt.show()
To include colormap in drawing, we can use colorbar() function.
Example: Use colorbar() function to add colormap to scatter graph.
import matplotlib.pyplot as plt
import numpy as np
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
colors = np.array([0, 10, 25, 38, 45])
plt.scatter(x_axis, y_axis, cmap = 'Dark2')
plt.colorbar()
plt.show()
Available ColorMaps
You can choose any color to work with scatter graph.
Name | Reverse |
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 |
Let’s take a look at anothre color from above table.
Example: Use colorbar() function to add colormap to scatter graph.
import matplotlib.pyplot as plt
import numpy as np
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
colors = np.array([0, 10, 25, 38, 45])
plt.scatter(x_axis, y_axis, cmap = 'ocean')
plt.colorbar()
plt.show()
Size
You can alter the size of dots by using the argument s.
As with colors, be sure that the array for sizes is of the same length of the arrays that are for x- and y-axis.
Example: Use colorbar() function to add colormap to scatter graph.
import matplotlib.pyplot as plt
import numpy as np
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
sizes = np.array([0, 10, 25, 38, 45])
plt.scatter(x_axis, y_axis, s = sizes)
plt.show()
Alpha
You can set the transparency of dots by using the argument alpha.
As with colors, be sure that the array for sizes is of the same length of the arrays that are for x- and y-axis.
Example: Set the transparency of each dots of scatter graph.
import matplotlib.pyplot as plt
import numpy as np
x_axis = np.array([10, 18, 25, 36, 48])
y_axis = np.array([20, 26, 17, 31, 29])
sizes = np.array([0, 10, 25, 38, 45])
plt.scatter(x_axis, y_axis, s = sizes, alpha = 0.8)
plt.show()
Combine Color Size and Alpha
Matplot allows combining a colormap with different sizes on the dots.
Example: Using randint() function to create random arrays with 50 values for x-points, y-points, colors and sizes.
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randint(50, size=(50))
y = np.random.randint(50, size=(50))
colors = np.random.randint(50, size=(50))
sizes = 10 * np.random.randint(50, size=(50))
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='RdGy_r')
plt.colorbar()
plt.show()