- 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
Adding a line to a scatter plot using Python's Matplotlib
To add a line to a scatter plot using Python's Matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Initialize a variable, n, for number of data points.
- Plot x and y data points using scatter() method.
- Plot a line using plot() method.
- Limt the X-axis using xlim() 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 n = 100 x = np.random.rand(n) y = np.random.rand(n) plt.scatter(x, y, c=x) plt.plot([0.1, 0.4, 0.3, 0.2]) plt.xlim(0, 1) plt.show()
Output
- Related Articles
- Adding caption below X-axis for a scatter plot using Matplotlib
- Adding a scatter of points to a boxplot using Matplotlib
- How to turn off transparency in Matplotlib's 3D Scatter plot?
- How to draw an average line for a scatter plot in MatPlotLib?
- How to overplot a line on a scatter plot in Python?
- Plot scatter points using plot method in Matplotlib
- How does parameters 'c' and 'cmap' behave in a Matplotlib scatter plot?
- Making matplotlib scatter plots from dataframes in Python's pandas
- Controlling the alpha value on a 3D scatter plot using Python and Matplotlib
- How to animate a scatter plot in Matplotlib?
- How can I draw a scatter trend line using Matplotlib?
- How to plot scatter masked points and add a line demarking masked regions in Matplotlib?
- How can Matplotlib be used to create three-dimensional scatter plot using Python?
- Connecting two points on a 3D scatter plot in Python and Matplotlib
- How to make a discrete colorbar for a scatter plot in matplotlib?

Advertisements