How to plot the data in Python

Import the necessary packages

The first step is to import matplotlib, which will be used to plot data on graphs:

import matplotlib.pyplot as plt

Syntax

This function is used to plot a graph:

plt.plot(x,y)

Parameters

There can be many parameters for this function defining its styles. We have to provide at least x and y coordinates.

Return value

This method returns a 2D graph.

class matplotlib.lines.Line2D(xdata, ydata, linewidth=None, linestyle=None, color=None, marker=None, markersize=None, markeredgewidth=None, markeredgecolor=None, markerfacecolor=None, markerfacecoloralt='none', fillstyle=None, antialiased=None, dash_capstyle=None, solid_capstyle=None, dash_joinstyle=None, solid_joinstyle=None, pickradius=5, drawstyle=None, markevery=None, **kwargs)

First basic plot

We can create test data that will be used as shown in the example below. We can get the output by clicking the “Run” button.

import matplotlib.pyplot as plt
# datasets x and y are lists, but they can also be, for instance, numpy arrays or pd.Series.
x = [1, 2, 3, 4, 5]
y = [25, 32, 34, 20, 25]
# plot
plt.plot(x, y)

In the code above:

  • Lines 5 and 6: We define x and y coordinates.

  • Line 8: We call the built-in function of matplotlib to plot a graph.

Change colors and styles

The output can be enhanced by changing its styles and colors, and parameters can be passed to the plt.plot(parameters..) function to depict the different styles. We can get the updated output by clicking the “Run” button in the example below:

import matplotlib.pyplot as plt
# datasets x and y are lists, but they can also be, for instance, numpy arrays or pd.Series.
x = [1, 2, 3, 4, 5]
y = [25, 32, 34, 20, 25]
# plot
plt.plot(x, y, color='red' , marker='o', markersize=20, linestyle='--', linewidth=4)

In the code above:

  • Line 8: We call the built-in function of matplotlib to plot a graph with different parameters for our styling.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved