Create a line plot in Matplotlib

Python

Matplotlib

Line

Create

Luc B.

Python

Matplotlib

Line Plot

Line plots are powerful tools for visualizing trends in ordered data. Matplotlib's plt.plot() function allows programmers to create these visualizations in just a couple lines of code.

Code Example

Use the plt.plot() function to create a line plot in Matplotlib.

import matplotlib.pyplot as plt

x_values = [0, 1, 2, 3, 4, 5, 6]
y_values = [0.27, 0.32, 0.35, 0.49, 0.55, 0.7, 0.95]

# Create line plot here
plt.plot(x_values, y_values)

plt.show()

png

plt.plot() is Matplotlib's swiss army knife function. While it's a perfect tool for creating line plots, by simply passing different parameters it can be used to create scatter plots, add emphasis and intrigue, and more. With plt.plot() you can:

More Examples

Object Oriented Interface

The same thing can be accomplished with Matplotlib's object oriented interface using the Axes.plot() method.

fig = plt.figure()
ax = plt.axes()

# Create a line plot here
ax.plot(x_values, y_values)

plt.show()

png