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 –
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')
Output:
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')
Output:
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
# 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')
Output:
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')
Output:
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 –
- Create a Scatter Plot in Python with Matplotlib
- Change Background Color of Plot in Matplotlib
- Add Trendline to a Maplotlib Plot with Code and Output
- How to Add Title to a Plot in Matplotlib? (Code Examples with Output)
- Set Axis Range (axis limits) in Matplotlib Plots
- Show Gridlines on Matplotlib Plots
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.