- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 get the trend line points.
Steps
Set the figure size and adjust the padding between and around the subplots.
Create x and y data points using numpy.
Create a figure and a set of subplots.
Plot x and y data points using numpy.
Find the trend line data points using polyfit() and poly1d() method.
Plot x and p(x) data points using plot() method.
To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.random.rand(100) y = np.random.rand(100) fig, ax = plt.subplots() _ = ax.scatter(x, y, c=x, cmap='plasma') z = np.polyfit(x, y, 1) p = np.poly1d(z) plt.plot(x, p(x), "r-o") plt.show()
Output
- Related Articles
- How can I draw inline line labels in Matplotlib?
- How to draw an average line for a scatter plot in MatPlotLib?
- How can I make a scatter plot colored by density in Matplotlib?
- How to draw rounded line ends using Matplotlib?
- Adding a line to a scatter plot using Python's Matplotlib
- How can I convert from scatter size to data coordinates in Matplotlib?
- How can I make a simple 3D line with Matplotlib?
- How can I create a stacked line graph with matplotlib?
- How can I cycle through line styles in Matplotlib?
- How can Matplotlib be used to create three-dimensional scatter plot using Python?
- How to plot scatter masked points and add a line demarking masked regions in Matplotlib?
- Can I give a border to a line in Matplotlib plot function?
- Adding a scatter of points to a boxplot using Matplotlib
- How to draw a line outside of an axis in Matplotlib?
- How to animate a scatter plot in Matplotlib?

Advertisements