Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to draw an average line for a scatter plot in MatPlotLib?
To draw an average line for a scatter plot in matplotlib, you can overlay a horizontal line representing the mean value of your data. This is useful for visualizing how individual data points compare to the overall average.
Basic Approach
The key steps are ?
Create your scatter plot with the original data points
Calculate the average (mean) of your y-values
Draw a horizontal line at the average y-value across the entire x-range
Add a legend to distinguish between the data points and average line
Example
Here's how to create a scatter plot with an average line ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data
x = np.array([3, 4, 5, 6, 7, 8, 9])
y = np.array([6, 5, 4, 3, 2, 1, 6])
# Create figure and axis
fig, ax = plt.subplots()
# Plot the scatter plot
ax.plot(x, y, 'o-', label='Data Points')
# Calculate and plot the average line
y_avg = np.mean(y) # Calculate mean of y values
ax.axhline(y=y_avg, color='red', linewidth=2, linestyle='--', label=f'Average (y={y_avg:.2f})')
# Add legend and display
plt.legend(loc='upper right')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Scatter Plot with Average Line')
plt.show()
The output shows the original data points connected by a line, with a red dashed horizontal line indicating the average y-value.
Alternative Method Using axhline()
Instead of creating an array of average values, you can use axhline() for a cleaner approach ?
import numpy as np
import matplotlib.pyplot as plt
# Sample data
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = np.array([2.1, 3.8, 2.9, 4.2, 5.1, 4.8, 6.2, 5.9, 7.1, 6.8])
# Create scatter plot
plt.figure(figsize=(8, 5))
plt.scatter(x, y, color='blue', alpha=0.7, s=50, label='Data Points')
# Add average line
y_mean = np.mean(y)
plt.axhline(y=y_mean, color='red', linestyle='--', linewidth=2,
label=f'Average = {y_mean:.2f}')
# Formatting
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Scatter Plot with Average Line')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Key Methods Comparison
| Method | Function | Best For |
|---|---|---|
| Array of averages | np.mean(y) * len(x) |
When you need the average as data points |
| axhline() | plt.axhline(y=mean_value) |
Simple horizontal lines (recommended) |
Conclusion
Use axhline() to draw clean average lines on scatter plots. Calculate the mean with np.mean() and include it in the legend label for clarity. This technique helps viewers quickly identify data points above or below the average.
