Related Tags

python
matplotlib
communitycreator

# How to set colors to a multiple scatter plot in matplotlib? Onyejiaku Theophilus Chidalu

### 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 plt
import numpy as np

# x-coordinates for both the scatter plots
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# the first scatter plot
y1 = np.array([10, 50, 30, 40, 50, 25, 70, 15, 90, 65])
plt.scatter(x, y1)

# the second scatter plot
y2 = np.array([5, 35, 40, 45, 80, 20, 95, 55, 70, 10])
plt.scatter(x,y2)

# displaying both plots
plt.show()

### Explanation

• Line 1: In matplotlib, the pyplot module is imported, which will be used to create plots.

• Line 2: The numpy module is imported, which will be used to create arrays.

• Line 5: The array x is created, containing the x-coordinates common to both plots.

• Lines 8 to 9: The array y1 is created, which contains the y-coordinates for the first scatter plot. pyplot.scatter(x,y1) is used to create a scatter plot of x and y1.

• Lines 12 to 13: The array y2 is created, which contains the y-coordinates for the second scatter plot. pyplot.scatter(x,y2) is used to create a scatter plot of x and y2.

• Line 16: The pyplot.show() function is used, which tells pyplot to display both the scatter plots.

Note: Notice that the two plots in the figure above gave two different colors. By default, pyplot returned orange and blue.

### 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 plt
import numpy as np

# x-coordinates for both the scatter plots
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# the first scatter plot
y1 = np.array([10, 50, 30, 40, 50, 25, 70, 15, 90, 65])
plt.scatter(x, y1, color = 'red')

# the second scatter plot
y2 = np.array([5, 35, 40, 45, 80, 20, 95, 55, 70, 10])
plt.scatter(x,y2, color = 'green')

# displaying both plots
plt.show()
Output of the code

RELATED TAGS

python
matplotlib
communitycreator

CONTRIBUTOR Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time 