Matplotlib.axes.Axes.plot() in Python
Last Updated :
02 Apr, 2025
Axes.plot() method in Matplotlib is used to plot data on a set of axes. It is primarily used for creating line plots but can be extended for other types of plots, including scatter plots, bar plots, and more. When using this function, you typically provide the x and y coordinates of the data points you want to plot. Example:
Python
import matplotlib.pyplot as plt
import numpy as np
# make an agg figure
fig, ax = plt.subplots()
ax.plot([1, 2, 3])
ax.set_title('matplotlib.axes.Axes.plot() example 1')
fig.canvas.draw()
plt.show()
Output:

Explanation: This code creates a line plot with the data [1, 2, 3], adds a title "matplotlib.axes.Axes.plot() example 1", and then displays the plot using plt.show(). The plot is drawn on an agg backend figure to render without displaying the window immediately until plt.show() is called.
Syntax
Axes.plot(self, *args, **kwargs)
Parameters:
- *args: Data points to be plotted. At a minimum, you need to pass the x and y values, but you can also pass additional data or specify other plot attributes.
- **kwargs: Additional keyword arguments for customization. These allow you to control aspects like color, line style, markers, labels, etc.
Return Value: This returns a list of Line2D objects representing the plotted data. These objects allow further customization or modification of the plotted lines after they have been created.
Examples of Matplotlib.axes.Axes.plot()
1. Line Plot with Date Formatting Using matplotlib.axes.Axes.plot()
This code demonstrates how to create a line plot with dates on the x-axis using matplotlib.axes.Axes.plot() in Python. It shows how to format date-time values and customize the x-axis with different date and time locators, formats, and labels.
Python
import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter, drange
import numpy as np
date1 = datetime.datetime(2000, 3, 2)
date2 = datetime.datetime(2000, 3, 6)
delta = datetime.timedelta(hours = 6)
dates = drange(date1, date2, delta)
y = np.arange(len(dates))
fig, ax = plt.subplots()
ax.plot_date(dates, y ** 2)
ax.set_xlim(dates[0], dates[-1])
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_minor_locator(HourLocator(range(0, 25, 6)))
ax.xaxis.set_major_formatter(DateFormatter('% Y-% m-% d'))
ax.fmt_xdata = DateFormatter('% Y-% m-% d % H:% M:% S')
fig.autofmt_xdate()
plt.title("Matplotlib Axes Class Example")
plt.show()
Output:

Explanation:
- The code uses drange() to generate a range of datetime values from March 2, 2000, to March 6, 2000, with a 6-hour interval.
- It then creates a simple line plot with these datetime values on the x-axis, and squares of the indices (y ** 2) as the y-values.
- The plot is customized to display dates on the x-axis, with major and minor ticks formatted using DayLocator() and HourLocator().
- The x-axis labels are formatted with DateFormatter('%Y-%m-%d'). The x-axis data formatting is set using ax.fmt_xdata, and the plot is displayed with the plt.show() method.
2. Plotting Multiple Lines with Random Data
This code demonstrates how to create multiple line plots with different data series using matplotlib.axes.Axes.plot() in Python. It also shows how to customize the color of the lines and set axis limits.
Python
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
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
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(xdata1, ydata1, color ='tab:blue')
ax.plot(xdata2, ydata2, color ='tab:orange')
# set the limits
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
ax.set_title('matplotlib.axes.Axes.plot() example 2')
plt.show()
Output:

Explanation:
- np.random.random() generates random data.
- ax.plot() plots two lines with different colors.
- ax.set_xlim() and ax.set_ylim() set the axis limits.
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read