How can I add an average line to a plot in Matplotlib? 2

How can I add an average line to a plot in Matplotlib?

Adding an average line to a plot in Matplotlib refers to the process of including a horizontal line that represents the average value of the data being plotted. This line can provide valuable insights into the overall trend and central tendency of the data. To add an average line, one can calculate the mean of the data and use Matplotlib’s “axhline” function to draw a straight line at that value. This can be done for both line and scatter plots, providing a visual representation of the average value and aiding in data analysis.

Add an Average Line to Plot in Matplotlib


You can use the following basic syntax to add an average line to a plot in Matplotlib:

import matplotlib.pyplotas plt
import numpy as np

#create scatter plot
plt.scatter(df.x, df.y)

#add horizontal line at mean value of y
plt.axhline(y=np.nanmean(df.y))

Note that axhline adds a horizontal line to the plot and nanmean calculates the average value (ignoring NaNs) where the line should be placed.

The following example shows how to use this syntax in practice.

Example: Add Average Line to Plot in Matplotlib

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
                   'y':[2, 5, 6, 5, 7, 8, 10, 12, 10, 9, 11, 15]})

#view first five rows of DataFrame
df.head()

	x	y
0	1	2
1	2	5
2	3	6
3	4	5
4	5	7

We can use the following code to create a scatter plot of x vs. y and add a horizontal line that represents the average y-value:

import matplotlib.pyplotas plt
import numpy as np

#create scatter plot
plt.scatter(df.x, df.y)

#add horizontal line at mean value of y
plt.axhline(y=np.nanmean(df.y))

We can see that an average line has been added to the plot just above the y-value of 8.

 If we calculate the average y-value, we’ll find that it’s 8.333:

#calculate average y-value
np.nanmean(df.y)

8.333333333

Note that we can also use the color, linestyle, and linewidth arguments to specify the color, line type, and line width of the average line, respectively:

import matplotlib.pyplotas plt
import numpy as np

#create scatter plot
plt.scatter(df.x, df.y)

#add horizontal line at mean value of y
plt.axhline(y=np.nanmean(df.y), color='red', linestyle='--', linewidth=3, label='Avg')

 

average line in Matplotlib

Additional Resources

The following tutorials explain how to perform other common tasks in Matplotlib:

How to Add a Trendline in Matplotlib

Cite this article

stats writer (2024). How can I add an average line to a plot in Matplotlib?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-an-average-line-to-a-plot-in-matplotlib/

stats writer. "How can I add an average line to a plot in Matplotlib?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-an-average-line-to-a-plot-in-matplotlib/.

stats writer. "How can I add an average line to a plot in Matplotlib?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-an-average-line-to-a-plot-in-matplotlib/.

stats writer (2024) 'How can I add an average line to a plot in Matplotlib?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-an-average-line-to-a-plot-in-matplotlib/.

[1] stats writer, "How can I add an average line to a plot in Matplotlib?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I add an average line to a plot in Matplotlib?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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