How to set colors to a multiple scatter plot in matplotlib?
Overview
The pyplot.scatter() function in matplotlib is used to create a scatter plot. The pyplot module has a function called scatter(), among many other functions, which helps to create or draw scatter plots.
Properties of a scatter plot
-
Uses dots to represent data points but does not join those points with lines.
-
Shows the relationship between two numeric variables. This relation can be used to perform data analysis.
Creating a multiple scatter plot
Before we look at how to set colors to a scatter plot, let’s look at how to create two scatter plots on the same graph.
As mentioned earlier, we use the pyplot.scatter() function in matplotlib, which takes two mandatory arguments:
-
An array of x-coordinates.
-
An array of y-coordinates.
Both of these arrays must be of the same length.
Code
import matplotlib.pyplot as pltimport numpy as np# x-coordinates for both the scatter plotsx = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])# the first scatter ploty1 = np.array([10, 50, 30, 40, 50, 25, 70, 15, 90, 65])plt.scatter(x, y1)# the second scatter ploty2 = np.array([5, 35, 40, 45, 80, 20, 95, 55, 70, 10])plt.scatter(x,y2)# displaying both plotsplt.show()
Explanation
-
Line 1: In
matplotlib, thepyplotmodule is imported, which will be used to create plots. -
Line 2: The
numpymodule is imported, which will be used to create arrays. -
Line 5: The array
xis created, containing the x-coordinates common to both plots. -
Lines 8 to 9: The array
y1is created, which contains the y-coordinates for the first scatter plot.pyplot.scatter(x,y1)is used to create a scatter plot ofxandy1. -
Lines 12 to 13: The array
y2is created, which contains the y-coordinates for the second scatter plot.pyplot.scatter(x,y2)is used to create a scatter plot ofxandy2. -
Line 16: The
pyplot.show()function is used, which tellspyplotto display both the scatter plots.
Note: Notice that the two plots in the figure above gave two different colors. By default,
pyplotreturnedorangeandblue.
Setting colors to the multiple scatter plot
To set the colors of a scatter plot, we need to set the argument color or simply c to the pyplot.scatter() function.
For example, take a look at the code below:
plt.scatter(x, y, color = 'red')
The above code means that we are setting the color of the scatter plot as red.
Code
import matplotlib.pyplot as pltimport numpy as np# x-coordinates for both the scatter plotsx = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])# the first scatter ploty1 = np.array([10, 50, 30, 40, 50, 25, 70, 15, 90, 65])plt.scatter(x, y1, color = 'red')# the second scatter ploty2 = np.array([5, 35, 40, 45, 80, 20, 95, 55, 70, 10])plt.scatter(x,y2, color = 'green')# displaying both plotsplt.show()