matplotlib basics

In [1]:
import matplotlib.pyplot as plt
import numpy as np

Basic example

In [2]:
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()

Two interfaces

In [3]:
x = np.arange(0, 10, 0.2)
y = np.sin(x)

OO API

In [6]:
fig, ax = plt.subplots(1, figsize=(9, 5))
ax.plot(x, y)
ax.set_xlim(0, 6)
ax.set_ylabel('Value')
plt.show()

matlab-style API

In [7]:
plt.figure(figsize=(9, 5))
plt.plot(x, y)
plt.xlim(0, 6)
plt.ylabel('Value')
plt.show()

In a Jupyter notebook

In [ ]:
import matplotlib.pyplot as plt
%matplotlib inline