Plotting using matplotlib

This section provides a brief introduction to plotting in pandas using matplotlib. The matplotlib API is imported using the standard convention, as shown in the following command:

In [1]: import matplotlib.pyplot as plt 

Series and DataFrame have a plot method, which is simply a wrapper around plt.plot. Here, we will examine how we can do a simple plot of a sine and cosine function. Suppose we wished to plot the following functions over the interval pi to pi:

  • f(x) = cos(x) + sin (x)
  • g(x) = cos (x) - sin (x)

This gives the following interval:

 In [51]: import numpy as np In [52]: X = np.linspace(-np.pi, np.pi, 256,endpoint=True) In [54]: f,g = np.cos(X)+np.sin(X), np.sin(X)-np.cos(X) In [61]: f_ser=pd.Series(f) ...

Get Mastering pandas - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.