Search⌘ K
AI Features

Plotting Multiple Curves

Explore how to create and customize multiple curves on a single graph using matplotlib's object-oriented techniques. Understand adding legends, managing figure sizes, and organizing multiple plots with subplots for clearer scientific data visualization.

In this course, we will be using the object-oriented approach when working with matplotlib.The main idea with object-oriented programming is to have objects that one can apply functions and actions to, and no object or program states should be global. The real advantage of the object-oriented approach in plotting becomes apparent when more than one figure is created, or when a figure contains more than one plot.

Multiple curves on the same graph

We can draw multiple curves on the same graph by repeatedly using the plot command.

Python 3.5
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2 * np.pi , 100)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1) # curve 1
plt.plot(x, y2) # curve 2
plt.plot(x, y1 + y2) # curve 3

If you don’t specify a ...