- How to Label Points on a Scatter Plot in Matplotlib?
- How to annotate points on a scatter plot in matplotlib?
- Example 1 – Label a point on the scatter plot
- Example 2 – Label Each Point on the Scatter Plot
- Author
- Matplotlib Label Scatter Points
- Add Label to Scatter Plot Points Using the matplotlib.pyplot.annotate() Function
- Add Label to Scatter Plot Points Using the matplotlib.pyplot.text() Function
- Related Article — Matplotlib Scatter Plot
- Scatter plots with a legend#
- Automated legend creation#
How to Label Points on a Scatter Plot in Matplotlib?
In this tutorial, we will look at how to label points on a scatter plot in Matplotlib with the help of some examples.
How to annotate points on a scatter plot in matplotlib?
You can use the matplotlib.pyplot.text() function to label points in a matplotlib scatter plot. The matplotlib.pyplot.text() function is used to add text at the location (x, y) in the plot. The following is the syntax –
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
Introductory ⭐
Intermediate ⭐⭐⭐
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
import matplotlib.pyplot as plt # add text, s to a point at (x, y) coordinate in a plot plt.text(x, y, s)
Now to add labels to each point in the scatter plot, use the matplotlib.pyplot.text() function for each point (x, y) and add its appropriate label.
Let’s now look at some examples of using the above syntax. First, we will create a simple scatter plot.
import matplotlib.pyplot as plt # x values - years x = [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020] # y values - 1 USD in INR y = [46.67, 53.44, 56.57, 62.33, 62.97, 66.46, 67.79, 70.09, 70.39, 76.38] # plot x and y on scatter plot plt.scatter(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('1USD in INR')
Example 1 – Label a point on the scatter plot
In case you only want to label a specific point (or points), use the matplotlib.pyplot.text() function only for those points. For example, let’s label the point (2014, 62.33) .
# plot x and y on scatter plot plt.scatter(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('1USD in INR') # add label to a point plt.text(2014, 62.33, 'USD crosses\n60 Rupee mark')
You can customize the way the text label appears by using additional arguments. For example, you can adjust the vertical and the horizontal alignment of the text, you can rotate the text, etc.
# plot x and y on scatter plot plt.scatter(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('1USD in INR') # add label to a point plt.text(2014, 62.33, 'USD crosses\n60 Rupee mark', va='center', ha='center')
Here, we used the va parameter to set the vertical alignment of the text to ‘center’ and the ha parameter to set the horizontal alignment to ‘center’ as well.
Example 2 – Label Each Point on the Scatter Plot
To label each point on the scatter plot, use the matplotlib.pyplot.text() function for each point in the plot.
# plot x and y on scatter plot plt.scatter(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('1USD in INR') # add labels to all points for (xi, yi) in zip(x, y): plt.text(xi, yi, yi, va='bottom', ha='center')
Here, we added the y-axis value of the point as its label for each point on the scatter plot.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Author
Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts
Data Science Parichay is an educational website offering easy-to-understand tutorials on topics in Data Science with the help of clear and fun examples.
Matplotlib Label Scatter Points
- Add Label to Scatter Plot Points Using the matplotlib.pyplot.annotate() Function
- Add Label to Scatter Plot Points Using the matplotlib.pyplot.text() Function
To label the scatter plot points in Matplotlib, we can use the matplotlib.pyplot.annotate() function, which adds a string at the specified position. Similarly, we can also use matplotlib.pyplot.text() function to add the text labels to the scatterplot points.
Add Label to Scatter Plot Points Using the matplotlib.pyplot.annotate() Function
matplotlib.pyplot.annotate(text, xy, *args, **kwargs)
It annotates the point xy with the value of the text parameter. xy represents a pair of coordinates (x, y) of the point to be annotated.
import numpy as np import matplotlib.pyplot as plt np.random.seed(20) X=np.random.randint(10, size=(5)) Y=np.random.randint(10, size=(5)) annotations=["Point-1","Point-2","Point-3","Point-4","Point-5"] plt.figure(figsize=(8,6)) plt.scatter(X,Y,s=100,color="red") plt.xlabel("X") plt.ylabel("Y") plt.title("Scatter Plot with annotations",fontsize=15) for i, label in enumerate(annotations): plt.annotate(label, (X[i], Y[i])) plt.show()
It creates two random arrays, X and Y , for X-coordinates and Y-coordinates of the points, respectively. We have a list called annotations with the same length as X and Y , which contains labels for each point. Finally, we iterate through a loop and use the annotate() method to add labels for each point in the scatter plot.
Add Label to Scatter Plot Points Using the matplotlib.pyplot.text() Function
matplotlib.pyplot.text(x, y, s, fontdict=None, **kwargs)
Here, x and y represent the coordinates where we need to place the text, and s is the content of the text that needs to be added.
The function adds text s at the point specified by x and y , where x represents the X coordinate of the point, and y represents the Y coordinate.
import numpy as np import matplotlib.pyplot as plt np.random.seed(20) X=np.random.randint(10, size=(5)) Y=np.random.randint(10, size=(5)) annotations=["Point-1","Point-2","Point-3","Point-4","Point-5"] plt.figure(figsize=(8,6)) plt.scatter(X,Y,s=100,color="red") plt.xlabel("X") plt.ylabel("Y") plt.title("Scatter Plot with annotations",fontsize=15) for i, label in enumerate(annotations): plt.text(X[i], Y[i],label) plt.show()
It iterates through a loop and uses the matplotlib.pyplot.text() method to add labels for each point in the scatter plot.
Suraj Joshi is a backend software engineer at Matrice.ai.
Related Article — Matplotlib Scatter Plot
Copyright © 2023. All right reserved
Scatter plots with a legend#
To create a scatter plot with a legend one may use a loop and create one scatter plot per item to appear in the legend and set the label accordingly.
The following also demonstrates how transparency of the markers can be adjusted by giving alpha a value between 0 and 1.
import numpy as np import matplotlib.pyplot as plt np.random.seed(19680801) fig, ax = plt.subplots() for color in ['tab:blue', 'tab:orange', 'tab:green']: n = 750 x, y = np.random.rand(2, n) scale = 200.0 * np.random.rand(n) ax.scatter(x, y, c=color, s=scale, label=color, alpha=0.3, edgecolors='none') ax.legend() ax.grid(True) plt.show()
Automated legend creation#
Another option for creating a legend for a scatter is to use the PathCollection.legend_elements method. It will automatically try to determine a useful number of legend entries to be shown and return a tuple of handles and labels. Those can be passed to the call to legend .
N = 45 x, y = np.random.rand(2, N) c = np.random.randint(1, 5, size=N) s = np.random.randint(10, 220, size=N) fig, ax = plt.subplots() scatter = ax.scatter(x, y, c=c, s=s) # produce a legend with the unique colors from the scatter legend1 = ax.legend(*scatter.legend_elements(), loc="lower left", title="Classes") ax.add_artist(legend1) # produce a legend with a cross-section of sizes from the scatter handles, labels = scatter.legend_elements(prop="sizes", alpha=0.6) legend2 = ax.legend(handles, labels, loc="upper right", title="Sizes") plt.show()
Further arguments to the PathCollection.legend_elements method can be used to steer how many legend entries are to be created and how they should be labeled. The following shows how to use some of them.
volume = np.random.rayleigh(27, size=40) amount = np.random.poisson(10, size=40) ranking = np.random.normal(size=40) price = np.random.uniform(1, 10, size=40) fig, ax = plt.subplots() # Because the price is much too small when being provided as size for ``s``, # we normalize it to some useful point sizes, s=0.3*(price*3)**2 scatter = ax.scatter(volume, amount, c=ranking, s=0.3*(price*3)**2, vmin=-3, vmax=3, cmap="Spectral") # Produce a legend for the ranking (colors). Even though there are 40 different # rankings, we only want to show 5 of them in the legend. legend1 = ax.legend(*scatter.legend_elements(num=5), loc="upper left", title="Ranking") ax.add_artist(legend1) # Produce a legend for the price (sizes). Because we want to show the prices # in dollars, we use the *func* argument to supply the inverse of the function # used to calculate the sizes from above. The *fmt* ensures to show the price # in dollars. Note how we target at 5 elements here, but obtain only 4 in the # created legend due to the automatic round prices that are chosen for us. kw = dict(prop="sizes", num=5, color=scatter.cmap(0.7), fmt="$ ", func=lambda s: np.sqrt(s/.3)/3) legend2 = ax.legend(*scatter.legend_elements(**kw), loc="lower right", title="Price") plt.show()
The use of the following functions, methods, classes and modules is shown in this example:
Total running time of the script: ( 0 minutes 1.882 seconds)