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:
Simple Plot in Python using Matplotlib
Next article icon

Simple Plot in Python using Matplotlib

Last Updated : 10 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Creating simple plots is a common step in data visualization. These visual representations help us to understand trends, patterns and relationships within data. Matplotlib is one of the most popular plotting libraries in Python which makes it easy to generate high-quality graphs with just a few lines of code. In this article, we'll see how to create basic plots using Matplotlib.

Before we start creating plots we need to install Matplotlib. We can install it using below command:

pip install matplotlib

Let's see an example of creating a Simple Plot

Python
import matplotlib.pyplot as plt  x = [1, 2, 3, 4, 5] y = [1, 4, 9, 16, 25]  plt.plot(x, y)  plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Simple Plot')  plt.show() 

Output:

simple-plot1

Syntax:

plt.plot(x, y, format_string, **kwargs)

Parameters:

  • x: A sequence of values to be plotted along the x-axis.
  • y: A sequence of values to be plotted along the y-axis.
  • format_string: A string that specifies the line style, color and markers. It is a optional parameter.
  • **kwargs: Other optional parameters such as label, linewidth, markersize, etc.

Key Matplotlib Functions for Simple Plots

MethodDescription
plot()This function creates a plot from the given data. It doesn’t display the plot but allows us to customize it.
show()This function displays the created plot. Without it the plot won't appear on the screen.
xlabel(),ylabel()These functions add labels to the x-axis and y-axis which helps in making our plot more understandable.
title()Adds a title to the plot to provide context.
gca()This function returns the current Axes object which is useful for modifying individual aspects of the plot such as spines, ticks, etc.
xticks(), yticks()Control where the ticks on the x-axis and y-axis appear. We can define the positions and step size.
legend()Adds a legend to the plot which is helpful when we have multiple data series in the same plot.
annotate()Adds annotations like text or markers at specific positions on the plot which is useful for highlighting key data points.
figure(figsize = (x, y)) Creates a figure with a specified size where x is the width and y is the height.
subplot(r, c, i)Creates multiple subplots in the same figure. r is the number of rows, c is the number of columns and i specifies the subplot's position.
set_xticks, set_yticksAllows customization of the range and step size of the ticks on the x-axis and y-axis useful in subplots.

Lets see various examples for customizing plots by using different functions.

Example 1: Creating a Customized Line Plot with Multiple Series

In this example, we’ll create a line plot with two different functions: y = x^2 and y = 30 - x^2. We will also customize the plot by adding gridlines, labels, title and legend.

Python
import matplotlib.pyplot as plt  x = [1, 2, 3, 4, 5] y1 = [1, 4, 9, 16, 25] y2 = [25, 20, 15, 10, 5]  plt.plot(x, y1, label='y = x^2', color='green', linestyle='--', marker='o') plt.plot(x, y2, label='y = 30 - x^2', color='red', linestyle='-', marker='x')  plt.grid(True)  plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Customized Line Plot with Multiple Series')  plt.legend() plt.savefig('all_features_plot.png') plt.show() 

Output:

simple-plot2
Customized Line

Example 2: Creating Multiple Subplots in One Figure

We will create four different subplots in a 2x2 grid. Each subplot will display a different dataset. This technique is useful when we want to compare multiple datasets side by side in a single figure.

Python
import matplotlib.pyplot as plt  a = [1, 2, 3, 4, 5] b = [0, 0.6, 0.2, 15, 10, 8, 16, 21] c = [4, 2, 6, 8, 3, 20, 13, 15]  fig = plt.figure(figsize =(10, 10))  sub1 = plt.subplot(2, 2, 1) sub2 = plt.subplot(2, 2, 2) sub3 = plt.subplot(2, 2, 3) sub4 = plt.subplot(2, 2, 4)  sub1.plot(a, 'sb')  sub1.set_xticks(list(range(0, 10, 1))) sub1.set_title('1st Rep')  sub2.plot(b, 'or')  sub2.set_xticks(list(range(0, 10, 2))) sub2.set_title('2nd Rep')  sub3.plot(list(range(0, 22, 3)), 'vg') sub3.set_xticks(list(range(0, 10, 1))) sub3.set_title('3rd Rep')  sub4.plot(c, 'Dm')  sub4.set_yticks(list(range(0, 24, 2))) sub4.set_title('4th Rep')  plt.show() 

Output:

basic-plot-2
Multiple Subplots in One Figure

Example 3: Plotting a Scatter Plot

Scatter plots are basic type of plot used to visualize the relationship between two variables. This is useful when we want to see how one variable is related to another or look for correlations.

Python
import matplotlib.pyplot as plt  x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10]  plt.scatter(x, y, color='blue', marker='x')  plt.xlabel('X-axis') plt.ylabel('Y-axis') plt.title('Simple Scatter Plot')  plt.show() 

Output:

basic-plot-4
Scatter Plot

With these simple techniques and examples we're now ready to start visualizing our data effectively using Matplotlib whether we're working with line plots, subplots or scatter plots.


Next Article
Simple Plot in Python using Matplotlib

P

parshavnahta97
Improve
Article Tags :
  • Python
  • Write From Home
  • Python-matplotlib
Practice Tags :
  • python

Similar Reads

    Multiplots in Python using Matplotlib
    Matplotlib is a Python library that can be used for plotting graphs and figures. Plotting multiplots or multiple plots are often required either for comparing the two curves or show some gradual changes in the multiple plots, and this can be done using Subplots. Subplots are one of the most importan
    3 min read
    Matplotlib.pyplot.title() in Python
    The title() method in the Matplotlib module is used to specify the title of the visualization depicted and display the title using various attributes. In this article, we will learn about this function with the help of examples.Syntax: matplotlib.pyplot.title(label, fontdict=None, loc='center', pad=
    3 min read
    Matplotlib.pyplot.sci() 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.semilogx() in Python
    Data Visualization Is an important part of analyzing the data as plotting graphs helps in providing better insight and understanding of the problem. Matplotlib.pyplot is one of the most commonly used libraries to do the same. It helps in creating attractive data and is super easy to use.  Matplotlib
    8 min read
    Matplotlib.pyplot.xlim() 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
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