How to Color Scatterplot by a variable in Matplotlib?


There are several ways to color scatterplot by a variable in Matplotlib of the python library. We have three parameters in scatter function namely cmap, alpha and c using which we can change the color of the plot.

Matplotlib is one of the libraries available in python which is used to plot and visualize the given data. This can be used as the extension of the Numpy library to plot the arrays. This library has the module called pyplot which makes the visualization and plotting the data very easy.

This pyplot module has many functions and parameters which helps us to plot the graphs in different and customized features. Let’s see each way one by one to color scatterplot by a variable in Matplotlib.

Using a colormap

A colormap is the range of colors that are used to represent a continuous variable. The colormap is represented in scatterplot by the cmap parameter.

Syntax

The following is the syntax of the colormap parameter of the scatterplot.

pyplot.scatter(x, y, cmap = color)

Where,

  • x and y are the arrays to be plotted.

  • cmap is the parameter to color the scatterplot

Example

In the following example, we are passing two arrays and the colormap value as the input parameters for the scatterplot function then the scatterplot will be plotted with the defined color.

import matplotlib.pyplot as plt
import numpy as np
x = np.array([20,30,40,70,50])
y = np.array([30,20,34,56,88])
z = np.random.rand(5)*10
plt.scatter(x, y, c = z, cmap = "viridis")
plt.show()

Output

Following is the output of the cmap parameter of the scatter() function to color scatterplot by a variable in matplotlib.

Using discrete set of colors

Here we will assign set of discrete values in which we can use a list of colors to assign one particular color to each value. We will pass the list of colors to the parameter c of the scatter function.

Syntax

The following is the syntax for using the c parameter of the scatter plot.

import matplotlib.pyplot as plt
plt.scatter(x, y, c=list_colors)

Where,

  • matplotlib.pyplot is the module in matplotlib.

  • plt is the alias name.

  • scatter is the function.

  • x, y are the two arrays

  • c is the parameter which contains list of colors

Example

In the following example, we will pass the two arrays and list of colors as the input parameters to the scatter() function.

import matplotlib.pyplot as plt
import numpy as np
x = np.array([[20,30,40],[70,50,89]])
y = np.array([[30,20,34],[5,6,88]])
z = np.random.rand(6)
plt.scatter(x,y,c = z)
plt.show()

Output

The output of the scatter plot plotted using the scatter() function with different colors to each value is as follows.

Using alpha (transparency)

We have the parameter alpha in the scatter() function which makes the points more or less transparent depending upon the variable. The value of alpha should be within the range of 0 and 1.

Syntax

The following is the syntax for using the alpha parameter of the scatter plot.

scatter(x,y,c = ‘color’, alpha = value)

Where,

  • scatter() is the function of matplotlib.pyplot module.

  • scatter()
  • x, y are the two arrays

  • c is the parameter which contains the color.

  • alpha is the e of the color which is in between 0 and 1

Example

When we pass two arrays, color for the c parameter and alpha value within rangthe range of 0 and 1 to the scatter() function of the matplotlib.pyplot then the plot will be plotted with the defined color varying based on the values.

import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
plt.scatter(x, y, c= 'green', alpha=z)
plt.show()

Output

The following is the output of the scatter() function alpha parameter.

Updated on: 09-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements