Search⌘ K

Plotting Multiple Curves

In this lesson, we will learn some different ways of plotting multiple graphs and how to label them.

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 color for a plotting statement, matplotlib will use its default colors. The first three default colors are special shades of blue, orange and green.

You can find more colors using the command `help(“matplotlib.pyplot.plot”) below:

Python 3.5
import matplotlib.pyplot as plt
help("matplotlib.pyplot.plot")
...