0
0
Matplotlibdata~5 mins

Trend lines on scatter plots in Matplotlib

Choose your learning style9 modes available
Introduction

Trend lines help us see the general direction or pattern in scattered data points. They make it easier to understand relationships between two variables.

You want to see if there is a positive or negative relationship between two measurements.
You want to predict values based on existing data points.
You want to summarize a large set of scattered points with a simple line.
You want to explain data trends in presentations or reports.
Syntax
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

# Create scatter plot
plt.scatter(x, y)

# Calculate trend line coefficients
coefficients = np.polyfit(x, y, degree)

# Create polynomial function from coefficients
poly_fn = np.poly1d(coefficients)

# Plot trend line
plt.plot(x, poly_fn(x), color='red')

plt.show()

Use np.polyfit to find the best fit line or curve. The degree is usually 1 for a straight line.

np.poly1d creates a function you can use to get y-values for any x.

Examples
This example draws a scatter plot and a straight trend line (degree 1) through the points.
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.scatter(x, y)
coefficients = np.polyfit(x, y, 1)
poly_fn = np.poly1d(coefficients)
plt.plot(x, poly_fn(x), color='red')
plt.show()
This example fits a cubic (degree 3) curve to noisy sine wave data.
Matplotlib
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 50)
y = np.sin(x) + np.random.normal(0, 0.2, 50)

plt.scatter(x, y)
coefficients = np.polyfit(x, y, 3)  # cubic trend line
poly_fn = np.poly1d(coefficients)
plt.plot(x, poly_fn(x), color='green')
plt.show()
Sample Program

This program creates a scatter plot of points and draws a red straight trend line showing the general upward trend.

Matplotlib
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.3, 2.9, 3.8, 4.5, 5.1, 5.8, 6.5, 7.1, 7.9, 8.4])

# Scatter plot
plt.scatter(x, y, label='Data points')

# Calculate linear trend line
coefficients = np.polyfit(x, y, 1)  # degree 1 for straight line
poly_fn = np.poly1d(coefficients)

# Plot trend line
plt.plot(x, poly_fn(x), color='red', label='Trend line')

# Add labels and legend
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Scatter Plot with Trend Line')
plt.legend()
plt.grid(True)
plt.show()
OutputSuccess
Important Notes

Make sure your x values are sorted or use np.sort(x) before plotting the trend line for a smooth line.

Higher degree polynomials can fit data better but may cause wiggly lines that overfit.

Always label your plots to make them easy to understand.

Summary

Trend lines show the overall direction in scattered data.

Use np.polyfit and np.poly1d to create trend lines.

Plot the trend line on top of scatter points to visualize relationships.