Open In App

Drawing Scatter Trend Lines Using Matplotlib

Last Updated : 30 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Matplotlib is a powerful Python library for data visualization, and one of its essential capabilities is creating scatter plots with trend lines. Scatter plots are invaluable for visualizing relationships between variables, and adding a trend line helps to highlight the underlying pattern or trend in the data. This article will guide you through the process of drawing scatter trend lines using Matplotlib, covering both linear and polynomial trend lines.

Drawing Scatter Trend Line Using Matplotlib

A scatter plot is a type of data visualization that uses dots to represent the values obtained for two different variables. The position of each dot on the horizontal and vertical axis indicates values for an individual data point. Scatter plots are used to observe relationships between variables.

Creating a Basic Scatter Plot:

Let's start by creating a basic scatter plot. We will use random data for simplicity.

Python
import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(50)
y = np.random.rand(50)

plt.scatter(x, y)
plt.title("Basic Scatter Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

Output:

scatter
Basic Scatter Plot

1. Adding a Linear Trend Line

A linear trend line is a straight line that best represents the data on a scatter plot. To add a linear trend line, we can use NumPy's polyfit() function to calculate the best-fit line.

Python
# Calculate the best-fit line
z = np.polyfit(x, y, 1)
p = np.poly1d(z)

# Plot the scatter plot and the trend line
plt.scatter(x, y)
plt.plot(x, p(x), "r--")  # 'r--' is for a red dashed line
plt.title("Scatter Plot with Linear Trend Line")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

Output:

scatter-withy-line
Linear Trend Line

2. Adding a Polynomial Trend Line

Sometimes, a linear trend line might not be sufficient to capture the relationship between variables. In such cases, a polynomial trend line can be more appropriate. We can use the polyfit() function with a higher degree.

Python
# Calculate the polynomial trend line (degree 2)
z = np.polyfit(x, y, 2)
p = np.poly1d(z)

# Plot the scatter plot and the polynomial trend line
plt.scatter(x, y)
plt.plot(x, p(x), "g-")  # 'g-' is for a green solid line
plt.title("Scatter Plot with Polynomial Trend Line")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

Output:

scatter-with-polynomial
Polynomial Trend Line

Customizing the Trend Line

Matplotlib allows extensive customization of plots, including the appearance of trend lines. You can modify the color, line style, and width of the trend line.

Python
# Calculate the best-fit line
z = np.polyfit(x, y, 1)
p = np.poly1d(z)

# Plot the scatter plot and the customized trend line
plt.scatter(x, y)
plt.plot(x, p(x), color="purple", linewidth=2, linestyle="--")
plt.title("Scatter Plot with Customized Trend Line")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

Output:

customized-trend-line
Customized Trend Line

Multiple Trend Lines

In some cases, you might want to compare different trend lines on the same scatter plot. This can be achieved by calculating and plotting multiple trend lines.

Python
# Generate random data
x = np.random.rand(50)
y = np.random.rand(50)

# Calculate the linear and polynomial trend lines
z1 = np.polyfit(x, y, 1)
p1 = np.poly1d(z1)
z2 = np.polyfit(x, y, 2)
p2 = np.poly1d(z2)

# Plot the scatter plot and both trend lines
plt.scatter(x, y)
plt.plot(x, p1(x), "r--", label="Linear Trend Line")
plt.plot(x, p2(x), "g-", label="Polynomial Trend Line")
plt.title("Scatter Plot with Multiple Trend Lines")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.show()

Output:

multiple-trend-lines
Multiple Trend Lines

Conclusion

Adding trend lines to scatter plots in Matplotlib is a powerful way to visualize and understand the relationships between variables. Whether you need a simple linear trend line or a more complex polynomial trend line, Matplotlib provides the tools necessary to create informative and visually appealing plots.


Next Article

Similar Reads