6. matplotlib

6.1. Matplotlib and Jupyter

  • To display matplotlib figures inline, you have run at least once per kernel run:

    %matplotlib inline
    

6.2. Points

../_images/matplotlib-01.png

Figure 25. Points chart

Listing 150. Matplotlib example
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 2, 3, 4]
plt.plot(x, y, 'o')
plt.show()

6.3. Sinusoid on grid

../_images/matplotlib-02.png

Figure 26. Sinusoid on grid

Listing 151. Matplotlib example
import matplotlib.pyplot as plt
import numpy as np

y = np.arange(0.0, 2.0, 0.01)
x = 1 + np.sin(2 * np.pi * y)

fig, ax = plt.subplots()

ax.plot(y, x)
ax.grid()
ax.set(
    xlabel='time (s)',
    ylabel='voltage (mV)',
    title='Voltage in Time')

plt.show()

6.4. Multiple charts

../_images/matplotlib-03.png

Figure 27. Multiple charts

Listing 152. Matplotlib example
import numpy as np
import matplotlib.pyplot as plt


def f(t):
    return np.exp(-t) * np.cos(2 * np.pi * t)


t1 = np.arange(0.0, 5.0, 0.1)
t2 = np.arange(0.0, 5.0, 0.02)

plt.figure(1)
plt.subplot(211)
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')

plt.subplot(212)
plt.plot(t2, np.cos(2 * np.pi * t2), 'r--')
plt.show()