Adding a Vertical Average Line to a Plot in Matplotlib

You can also add an average line in a plot in matplotlib vertically, i.e, along the y axis.

To add a vertical average line in a plot in matplotlib, you simply have to use the axvline function instead of the axhline function. Further, we will need to find the average of values along the x axis this time, so we will use ‘x.np.nanmean(df.x)’ as the argument for axvline function.

Here is the code for the same:

Python3




#import pandas
import pandas as pd
 
#import numpy
import numpy as np
 
#prepare the dataframe
df = pd.DataFrame({'x' : [2, 4, 5, 7, 9, 10, 11, 13, 14, 16],
                   'y' : [0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9]})
 
#import pyplot from matplotlib
import matplotlib.pyplot as plt
 
#create scatter plot
plt.scatter(df.x, df.y)
 
#add vertical average line
plt.axvline(x=np.nanmean(df.x))
 
#see the plot
plt.show()


Output :

output

Let us now understand the axvline function in detail.

The axvline function

The axvline function as the name suggests, draws a vertical line along the axis.

Python3




matplotlib.pyplot.axvline(x=0, ymin=0, ymax=1, **kwargs)


In the code, we have passed only one argument that gives the x position of the line. You can further use other parameters just like we did with axhline function, to change the line color, style, and width of the line. Here is the code and corresponding output for the same:

Python3




#import pandas
import pandas as pd
 
#import numpy
import numpy as np
 
#prepare the dataframe
df = pd.DataFrame({'x' : [2, 4, 5, 7, 9, 10, 11, 13, 14, 16],
                   'y' : [0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9]})
 
#import pyplot from matplotlib
import matplotlib.pyplot as plt
 
#create scatter plot
plt.scatter(df.x, df.y)
 
#add vertical average line
plt.axvline(
  x=np.nanmean(df.x),
  color = 'g',
  linestyle = '--',
  linewidth = 3
)
 
#see the plot
plt.show()


Output:

output

In this post, we learned how we can add an average line to a plot in matplotlib. You can use different functions from the matplotlib library of Python to add a horizontal as well as a vertical line to a plot in matplotlib. Further, you can change how the line appears by using the parameters provided with the axhline and axvline functions.



How to Add an Average Line to Plot in Matplotlib

In this article, we will learn how we can add an average line to a plot in matplotlib. We will discuss the steps to add a horizontal average line using the axhline function, as well as the steps to add a vertical average line using the axvline function in Matplotlib. Throughout the article, we will use examples and code so that you can easily implement the learnings.

Similar Reads

Adding a Horizontal Average Line to a Plot in Matplotlib

Before we begin with adding an average line to a plot in Matplotlib, let us first prepare a sample Matplotlib scatterplot. Here is the code for the same:...

Adding a Vertical Average Line to a Plot in Matplotlib

...

Contact Us