GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Simple Plot in Python using Matplotlib - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Simple Plot in Python using Matplotlib

Last Updated : 12 Jul, 2025

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.

Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.