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
Selected Reading
How can I draw a scatter trend line using Matplotlib?
To draw a scatter trend line using Matplotlib, we can use polyfit() and poly1d() methods to calculate and plot the trend line over scattered data points.
Basic Scatter Plot with Trend Line
Here's how to create a scatter plot with a linear trend 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.random.rand(100)
y = np.random.rand(100)
# Create scatter plot
fig, ax = plt.subplots()
ax.scatter(x, y, c=x, cmap='plasma', alpha=0.7)
# Calculate trend line using polyfit (degree 1 for linear)
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
# Sort x values for smooth line
x_sorted = np.sort(x)
plt.plot(x_sorted, p(x_sorted), "r-", linewidth=2, label='Trend Line')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Scatter Plot with Trend Line')
plt.legend()
plt.show()
Using Real Data with Correlation
Here's an example with data that has a clearer trend ?
import numpy as np
import matplotlib.pyplot as plt
# Create correlated data
np.random.seed(42)
x = np.linspace(0, 10, 50)
y = 2 * x + 1 + np.random.normal(0, 2, 50) # Linear relationship with noise
# Create scatter plot
plt.figure(figsize=(8, 6))
plt.scatter(x, y, alpha=0.6, color='blue', label='Data Points')
# Calculate and plot trend line
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x, p(x), "r-", linewidth=2, label=f'Trend: y = {z[0]:.2f}x + {z[1]:.2f}')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Scatter Plot with Linear Trend Line')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
print(f"Trend line equation: y = {z[0]:.2f}x + {z[1]:.2f}")
Trend line equation: y = 2.01x + 0.85
Polynomial Trend Lines
You can also create higher-degree polynomial trend lines ?
import numpy as np
import matplotlib.pyplot as plt
# Create data with quadratic relationship
x = np.linspace(-3, 3, 50)
y = x**2 + 2*x + 1 + np.random.normal(0, 1, 50)
plt.figure(figsize=(10, 6))
plt.scatter(x, y, alpha=0.6, color='green', label='Data Points')
# Linear trend line
z1 = np.polyfit(x, y, 1)
p1 = np.poly1d(z1)
# Quadratic trend line
z2 = np.polyfit(x, y, 2)
p2 = np.poly1d(z2)
# Plot both trend lines
x_smooth = np.linspace(x.min(), x.max(), 100)
plt.plot(x_smooth, p1(x_smooth), "r-", label='Linear Trend', linewidth=2)
plt.plot(x_smooth, p2(x_smooth), "orange", label='Quadratic Trend', linewidth=2)
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Linear vs Quadratic Trend Lines')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Key Parameters
| Function | Purpose | Key Parameter |
|---|---|---|
polyfit(x, y, deg) |
Calculate polynomial coefficients |
deg - polynomial degree (1=linear, 2=quadratic) |
poly1d(coefficients) |
Create polynomial function | Takes coefficients from polyfit |
scatter() |
Create scatter plot |
alpha - transparency, c - colors |
Conclusion
Use polyfit() and poly1d() to create trend lines over scatter plots. Choose degree 1 for linear trends or higher degrees for curved relationships. Sort x values for smooth trend line visualization.
Advertisements
