4.2. Line plots#

The most common command of Matplotlib’s pyplot module is probably plt.plot. This function plots one or many series expressed either as standard Python lists, as NumPy Arrays or as Pandas DataFrames. For unidimensional data, it takes one list (y) or two lists (x, y) as arguments.

For exemple, to plot this list: [1.0, 2.0, -1.0, -0.0]:

import matplotlib.pyplot as plt

y = [1.0, 2.0, -1.0, -0.0]
plt.plot(y)

Fig. 4.2 A Matplotlib figure window.#

You should normally see the plot in a new window, as shown in Fig. 4.2. If, instead, you see the figure inline, or in Spyder’s Graph pane, or not at all, then you probably did not configure Matplotlib for interactive graphics. See Configuring Spyder.

The different buttons on the figure allow some interaction with the plot:

Move the figure around (panning)

Zoom

Undo the last pan/zoom operation

Redo the last pan/zoom operation

Reset to the initial pan/zoom

Save the figure to an image file.

By default, x starts at 0 and increments by 1 for each point. But we can also specify custom coordinates for x:

import matplotlib.pyplot as plt

y = [1.0, 2.0, -1.0, -0.0]
x = [0.0, 5.0, 10.0, 15.0]

plt.plot(x, y);
_images/99be3e1a6a2b3391ade0cb8f33b8d08efb8508d3093a296e5695de0f8c1d6c2d.png

The x values don’t have to be equidistant:

x = [0.0, 0.5, 1, 10]

plt.plot(x, y);
_images/f6e11ff31b7475daa6e09162c9ac5e100fb4ede48bae66b7bf4eaa71acec82fd.png