matplotlib basics

import matplotlib.pyplot as plt
import numpy as np

Basic example

x = np.arange(0, 10, 0.2)
y1, y2 = np.sin(x), np.cos(x)

fig, ax = plt.subplots(2, figsize=(9, 6))
ax[0].plot(x, y1)
ax[1].plot(x, y2)
plt.show()

png

Two interfaces

x = np.arange(0, 10, 0.2)
y = np.sin(x)
OO API
fig, ax = plt.subplots(1, figsize=(9, 5))
ax.plot(x, y)
ax.set_xlim(0, 6)
ax.set_ylabel('Value')
plt.show()

png

matlab-style API
plt.figure(figsize=(9, 5))
plt.plot(x, y)
plt.xlim(0, 6)
plt.ylabel('Value')
plt.show()

png

In a Jupyter notebook

import matplotlib.pyplot as plt
%matplotlib inline

If you spot any errors please let me know on twitter.