Python:Matplotlib .plot()

msafarookhi's avatar
Published Dec 3, 2025
Contribute to Docs

The .plot() function in Matplotlib is the primary method used to create line plots and marker plots. It takes one or two sets of data points (x and y coordinates) and draws lines connecting them, displays markers at each point, or both. This function is one of the most commonly used tools in data visualization for showing trends, patterns, and relationships between variables.

Line plots are ideal for illustrating how data changes over time or across continuous intervals. It’s widely used in fields such as statistics, engineering, and data science for exploratory data analysis and model visualization.

  • Learn to clean, analyze, and visualize data with Python and SQL.
    • Includes 15 Courses
    • With Certificate
    • Beginner Friendly.
      55 hours
  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours

Syntax

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

Parameters:

  • *args: Values for y, or x and y, optionally followed by a format string.
  • scalex: Auto-scale the x-axis (default True).
  • scaley: Auto-scale the y-axis (default True).
  • data: Optional dict or DataFrame for referencing variables by name.
  • **kwargs: Style and configuration options like color, label, linewidth, marker.

Return value:

Returns a list of Line2D objects representing the plotted data.

Example: Creating a Basic Line Plot

This example demonstrates how to create a simple line plot with plt.plot() to visualize the relationship between two numerical variables:

import matplotlib.pyplot as plt
import numpy as np
# Sample data
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 1, 6, 3])
# Create a line plot
plt.plot(x, y)
# Add labels and a title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Basic Line Plot Example')
# Add a grid for better readability
plt.grid(True, linestyle='--', alpha=0.6)
# Display the plot
plt.show()

This example visualizes the relationship between an independent variable, X-axis, and a dependent variable, Y-axis, using a single continuous line. The plot shows the change in Y values as X increases from 1.0 to 5.0. This plot highlights a few useful details worth noting:

  • Single Series: The plot displays one series of data, represented by a single blue line, showing the progression of the Y-axis value relative to the X-axis value.
  • Data Points and Trend: The line connects the following data points: $(1.0, 2.0)$, $(2.0, 4.0)$, $(3.0, 1.0)$, $(4.0, 6.0)$, and $(5.0, 3.0)$. The plot illustrates clear changes in direction: an initial increase, a sharp decrease, a significant increase to the maximum value, and a final decrease.
  • Labels and Title: The plot is clearly titled “Basic Line Plot Example” and has “X-axis” and “Y-axis” labels for clarity.
  • Grid: A light dashed grid is present, running horizontally and vertically across the plot, which aids in precisely reading the coordinate values of the data points off the chart.

Line plot illustrating a fluctuating, piecewise linear trend between the X-axis and Y-axis, with a single blue line connecting 5 data points, a grid, and labeled axes

All contributors

Contribute to Docs

Learn Python:Matplotlib on Codecademy

  • Learn to clean, analyze, and visualize data with Python and SQL.
    • Includes 15 Courses
    • With Certificate
    • Beginner Friendly.
      55 hours
  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours