Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Pandas
  • Numpy
  • Seaborn
  • Ploty
  • Data visualization
  • Data Analysis
  • Power BI
  • Tableau
  • Machine Learning
  • Deep Learning
  • NLP
  • Computer Vision
  • Data Science for Beginners
  • Data Science interview questions
  • Data analysis interview questions
  • NLP Interview questions
Open In App
Next Article:
Matplotlib.pyplot.plot() function in Python
Next article icon

Matplotlib.pyplot.plot() function in Python

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The matplotlib.pyplot.plot() is used to create 2D plots such as line graphs and scatter plots. The plot() function allows us to plot data points, customize line styles, markers and colors making it useful for various types of visualizations. In this article, we'll see how to use this function to plot data in Python.

Syntax: matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)

Parameters:

  • x, y: Represent horizontal and vertical coordinates for the data points.
  • fmt: A format string that defines the line style, marker and colour.
  • data: The optional parameter can be an object containing labelled data which makes it easier to handle datasets directly.

Returns: A list of Line2D objects each representing a segment of the plotted data.

There are many ways of creating plot using Matplotlib.pyplot.plot() function some of their examples are:

Example 1: Line Plots in Matplotlib

Here we use Matplotlib's plot() function to create a simple line plot with the data [1, 2, 3, 4, 5].

Python
import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4, 5]) plt.title('Basic Line Plot') plt.show() 

Output:

matplot1
Basic Line Plot

Example 2: Multiple Lines Using Matplotlib

We will plot sine and cosine functions on the same graph.

  • plt.plot(x, y1, label='Sin(x)', color='blue'): Plots sine curve with x values and y1 values labeling it as "Sin(x)" and using a blue color for the line.
  • plt.plot(x, y2, label='Cos(x)', color='red', linestyle='--'): Plots cosine curve with x values and y2 values labeling it as "Cos(x)" using a red color and a dashed line style.
Python
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2 * np.pi, 100) y1 = np.sin(x) y2 = np.cos(x) plt.plot(x, y1, label='Sin(x)', color='blue') plt.plot(x, y2, label='Cos(x)', color='red', linestyle='--') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Multiple Lines Plot') plt.legend() plt.show() 

Output:

matplot2
Multiple Lines Plot

Example 3: Scatter Plot with Custom Markers

We will generate and customize scatter plot with 50 random data points featuring red circular markers.

  • plt.plot(x, y, marker='o', linestyle='', color='red', label='Scatter Plot'): Plots a scatter plot with x and y values and using red circular markers (marker='o') with no connecting lines (linestyle='') and labeling it as "Scatter Plot".
Python
import matplotlib.pyplot as plt import numpy as np np.random.seed(42) x = np.random.rand(50) y = np.random.rand(50) plt.plot(x, y, marker='o', linestyle='', color='red', label='Scatter Plot') plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Scatter Plot Example') plt.legend() plt.show() 

Output:

matplot3
Scatter Plot with Multiple Markers

Example 4: Plotting Multiple Curves

We are creating a line plot with two curves: a blue curve y = x^2and an orange curve y = 1 - x^3 generating data randomly. The plot is limited to the range [0, 1] on both axes showcasing a visual representation of mathematical functions.

Python
import matplotlib.pyplot as plt  import numpy as np  np.random.seed(19680801)  xdata = np.random.random([2, 10])  xdata1 = xdata[0, :]  xdata2 = xdata[1, :]  xdata1.sort()  xdata2.sort()  ydata1 = xdata1 ** 2 ydata2 = 1 - xdata2 ** 3 plt.plot(xdata1, ydata1, color ='tab:blue')  plt.plot(xdata2, ydata2, color ='tab:orange')  plt.xlim([0, 1])  plt.ylim([0, 1])  plt.title('matplotlib.pyplot.plot() example 2')  plt.show() 

Output:

matplot4
Two Curves Plot

With the flexibility to customize line styles, markers and colors Matplotlib's plot() function provides various possibilities for visualizing our data in Python.


Next Article
Matplotlib.pyplot.plot() function in Python

S

SHUBHAMSINGH10
Improve
Article Tags :
  • Python
  • Python-Library
  • Python-matplotlib
  • Matplotlib Pyplot-class
Practice Tags :
  • python

Similar Reads

    Matplotlib.pyplot.pcolor() function in Python
    Matplotlib is the well-known Python package used in data visualization. Numpy is the numerical mathematics extension of Matplotlib. Matplotlib is capable of producing high-quality graphs, charts, and figures. Matplotlib produces object-oriented API for embedding plots into projects using GUI toolkit
    2 min read
    Matplotlib.pyplot.plot_date() function in Python
    Matplotlib is a module package or library in Python which is used for data visualization. Pyplot is an interface to a Matplotlib module that provides a MATLAB-like interface. The matplotlib.pyplot.plot_date() function is like the regular plot() function, but it's tailored for showing data over dates
    3 min read
    Matplotlib.pyplot.setp() function in Python
    Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
    2 min read
    Matplotlib.pyplot.xscale() function in Python
    Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot,
    2 min read
    Matplotlib.pyplot.figimage() function in Python
    Matplotlib is a widely used library in Python for plotting various graphs, as it provides very efficient ways and easy to understand methods for complex plots also. pyplot is a collection of command style functions that make matplotlib work like MATLAB. figimage() function matplotlib.pyplot.figimage
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences