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
Adding a line to a scatter plot using Python's Matplotlib
To add a line to a scatter plot using Python's Matplotlib, you can combine the scatter() method for plotting points with the plot() method for drawing lines. This is useful for showing trends, reference lines, or connections between data points.
Basic Steps
- Set the figure size and adjust the padding between and around the subplots
- Initialize variables for your data points
- Plot x and y data points using
scatter()method - Add a line using
plot()method - Set axis limits using
xlim()andylim()methods - Display the figure using
show()method
Example
Here's how to create a scatter plot with an overlaid 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
# Generate random data points
n = 100
x = np.random.rand(n)
y = np.random.rand(n)
# Create scatter plot
plt.scatter(x, y, c=x, alpha=0.6)
# Add a line
plt.plot([0.1, 0.4, 0.6, 0.8], [0.2, 0.5, 0.3, 0.7],
color='red', linewidth=2, label='Trend Line')
# Set axis limits
plt.xlim(0, 1)
plt.ylim(0, 1)
# Add labels and legend
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Scatter Plot with Line')
plt.legend()
plt.show()
Adding Different Types of Lines
Straight Reference Line
You can add a diagonal reference line or any mathematical function ?
import numpy as np
import matplotlib.pyplot as plt
# Generate sample data
x = np.random.rand(50)
y = 2 * x + np.random.rand(50) * 0.3
# Create scatter plot
plt.scatter(x, y, alpha=0.6)
# Add diagonal reference line (y = 2x)
x_line = np.linspace(0, 1, 100)
y_line = 2 * x_line
plt.plot(x_line, y_line, 'r--', label='y = 2x')
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Scatter Plot with Reference Line')
plt.legend()
plt.show()
Best Fit Line
You can also add a trend line using polynomial fitting ?
import numpy as np
import matplotlib.pyplot as plt
# Generate correlated data
np.random.seed(42)
x = np.random.rand(30)
y = 2 * x + 0.5 + np.random.rand(30) * 0.3
# Create scatter plot
plt.scatter(x, y, alpha=0.7)
# Fit and plot trend line
coefficients = np.polyfit(x, y, 1)
polynomial = np.poly1d(coefficients)
x_line = np.linspace(x.min(), x.max(), 100)
plt.plot(x_line, polynomial(x_line), 'g-', linewidth=2,
label=f'Best Fit: y = {coefficients[0]:.2f}x + {coefficients[1]:.2f}')
plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Scatter Plot with Best Fit Line')
plt.legend()
plt.show()
Line Styling Options
| Parameter | Options | Purpose |
|---|---|---|
linestyle |
'-', '--', '-.', ':' | Line pattern |
linewidth |
1, 2, 3, etc. | Line thickness |
color |
'red', 'blue', '#FF0000' | Line color |
alpha |
0.0 to 1.0 | Transparency |
Conclusion
Adding lines to scatter plots helps visualize trends and relationships in your data. Use plot() after scatter() to overlay lines, and consider using polynomial fitting for best-fit trend lines.
Advertisements
