Drawing Scatter Trend Lines Using Matplotlib
Last Updated :
23 Jul, 2025
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:
Basic Scatter Plot1. 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:
Linear Trend Line2. 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:
Polynomial Trend LineCustomizing 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 LineMultiple 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 LinesConclusion
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.
Similar Reads
Python - Data visualization tutorial Data visualization is a crucial aspect of data analysis, helping to transform analyzed data into meaningful insights through graphical representations. This comprehensive tutorial will guide you through the fundamentals of data visualization using Python. We'll explore various libraries, including M
7 min read
What is Data Visualization and Why is It Important? Data visualization uses charts, graphs and maps to present information clearly and simply. It turns complex data into visuals that are easy to understand.With large amounts of data in every industry, visualization helps spot patterns and trends quickly, leading to faster and smarter decisions.Common
4 min read
Data Visualization using Matplotlib in Python Matplotlib is a widely-used Python library used for creating static, animated and interactive data visualizations. It is built on the top of NumPy and it can easily handles large datasets for creating various types of plots such as line charts, bar charts, scatter plots, etc. Visualizing Data with P
11 min read
Data Visualization with Seaborn - Python Seaborn is a popular Python library for creating attractive statistical visualizations. Built on Matplotlib and integrated with Pandas, it simplifies complex plots like line charts, heatmaps and violin plots with minimal code.Creating Plots with SeabornSeaborn makes it easy to create clear and infor
9 min read
Data Visualization with Pandas Pandas is a powerful open-source data analysis and manipulation library for Python. The library is particularly well-suited for handling labeled data such as tables with rows and columns. Pandas allows to create various graphs directly from your data using built-in functions. This tutorial covers Pa
6 min read
Plotly for Data Visualization in Python Plotly is an open-source Python library designed to create interactive, visually appealing charts and graphs. It helps users to explore data through features like zooming, additional details and clicking for deeper insights. It handles the interactivity with JavaScript behind the scenes so that we c
12 min read
Data Visualization using Plotnine and ggplot2 in Python Plotnine is a Python data visualization library built on the principles of the Grammar of Graphics, the same philosophy that powers ggplot2 in R. It allows users to create complex plots by layering components such as data, aesthetics and geometric objects.Installing Plotnine in PythonThe plotnine is
6 min read
Introduction to Altair in Python Altair is a declarative statistical visualization library in Python, designed to make it easy to create clear and informative graphics with minimal code. Built on top of Vega-Lite, Altair focuses on simplicity, readability and efficiency, making it a favorite among data scientists and analysts.Why U
4 min read
Python - Data visualization using Bokeh Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots. Bokeh output can be obtained in various mediums like notebook, html and server. It is possible to embed bokeh plots in Django and flask apps. Bokeh provides two visualization interfaces to us
4 min read
Pygal Introduction Python has become one of the most popular programming languages for data science because of its vast collection of libraries. In data science, data visualization plays a crucial role that helps us to make it easier to identify trends, patterns, and outliers in large data sets. Pygal is best suited f
5 min read