How can I add a legend to a scatterplot using Matplotlib? 2

How can I add a legend to a scatterplot using Matplotlib?

Adding a legend to a scatterplot using Matplotlib is a simple process that allows for clear and concise communication of the data being presented. A legend is a visual aid that explains the meaning of the different elements or variables in a plot. To add a legend to a scatterplot using Matplotlib, one can utilize the “legend” function and specify the labels and associated colors for each data point. This will create a visual key that helps viewers understand the relationship between the plotted data points and their respective labels. By incorporating a legend, the scatterplot becomes more informative and easier to interpret, making it a valuable tool in data visualization.

Add Legend to Scatterplot in Matplotlib


You can use the following syntax to add a legend to a scatterplot in Matplotlib:

import matplotlib.pyplotas plt
from matplotlib.colorsimport ListedColormap

#define values, classes, and colors to map 
values = [0, 0, 1, 2, 2, 2]
classes = ['A', 'B', 'C']
colors = ListedColormap(['red', 'blue', 'purple'])

#create scatterplot
scatter = plt.scatter(x, y, c=values, cmap=colors)

#add legend
plt.legend(*scatter.legend_elements())

The following examples show how to use this syntax in practice.

Example 1: Scatterplot Legend with Values

The following example shows how to create a scatterplot where the legend displays values:

import matplotlib.pyplotas plt
from matplotlib.colorsimport ListedColormap

#define data
x = [3, 4, 4, 6, 8, 9]
y = [12, 14, 17, 16, 11, 13]

#define values, classes, and colors to map 
values = [0, 0, 1, 2, 2, 2]
classes = ['A', 'B', 'C']
colors = ListedColormap(['red', 'blue', 'purple'])

#create scatterplot
scatter = plt.scatter(x, y, c=values, cmap=colors)

#add legend with values
plt.legend(*scatter.legend_elements())

Example 2: Scatterplot Legend with Classes

The following example shows how to create a scatterplot where the legend displays class names:

import matplotlib.pyplotas plt
from matplotlib.colorsimport ListedColormap

#define data
x = [3, 4, 4, 6, 8, 9]
y = [12, 14, 17, 16, 11, 13]

#define values, classes, and colors to map 
values = [0, 0, 1, 2, 2, 2]
classes = ['A', 'B', 'C']
colors = ListedColormap(['red', 'blue', 'purple'])

#create scatterplot
scatter = plt.scatter(x, y, c=values, cmap=colors)

#add legend with class names
plt.legend(handles=scatter.legend_elements()[0], labels=classes)

Notice that this legend displays the class names that we specified (A, B, C) as opposed to the values (0, 1, 2) that we specified.

Cite this article

stats writer (2024). How can I add a legend to a scatterplot using Matplotlib?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-a-legend-to-a-scatterplot-using-matplotlib/

stats writer. "How can I add a legend to a scatterplot using Matplotlib?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-a-legend-to-a-scatterplot-using-matplotlib/.

stats writer. "How can I add a legend to a scatterplot using Matplotlib?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-a-legend-to-a-scatterplot-using-matplotlib/.

stats writer (2024) 'How can I add a legend to a scatterplot using Matplotlib?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-a-legend-to-a-scatterplot-using-matplotlib/.

[1] stats writer, "How can I add a legend to a scatterplot using Matplotlib?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I add a legend to a scatterplot using Matplotlib?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top