In this tutorial, we will look at how to add an average line to a matplotlib plot with the help of some examples.

## What is an average line on a plot?

Generally, an average line is a (horizontal) line that represents the average value of the data points on the y-axis. Thus, to plot an average line (irrespective of the tool or library), first, find out the average y-value and then plot a horizontal line for that y-value.

## Average line in matplotlib

**To add an average line to a plot in matplotlib, you can use the matplotlib.pyplot.axhline() function and pass the average y-value as an argument.** You can use the

`numpy.mean()`

function to get the average of the y-values. The following is the syntax –import numpy as np import matplotlib.pyplot as plt # add an average line to a plot plt.axhline(np.mean(y_values))

Let’s now look at some examples of using the above syntax –

First, we will create a scatter plot to which we will later add an average line.

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:

The above scatter plot depicts the price of 1 USD in INR from 2011 to 2020. Let’s add an average line to this plot, which will depict the average USD to INR conversion rate from 2011 to 2020.

import numpy as np # plot x and y on scatter plot plt.scatter(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('1USD in INR') # add an average line plt.axhline(np.mean(y))

Output:

**Data Science Programs By Skill Level**

**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.

You can see that the horizontal line is around 63.

You can customize the horizontal line with additional parameters. For example, let’s make the line dashed and give it a green color.

import numpy as np # plot x and y on scatter plot plt.scatter(x, y) # add axes labels plt.xlabel('Year') plt.ylabel('1USD in INR') # add an average line plt.axhline(np.mean(y), linestyle='dashed', color='g')

Output:

You might also be interested in –

- Add Trendline to a Maplotlib Plot with Code and Output
- How to make text italic in a Matplotlib plot?
- How to bold text in a Matplotlib plot?
- Matplotlib – Remove the frame without altering the ticks and the tick labels
- Add Title to Each Subplot in Matplotlib

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**