Calmcode - matplotlib: api


1 2 3 4 5 6 7 8 9

Sofar we've only really shown you the "simple" usage of matplotlib. This includes a "use one-liners where possible" attitude but it is worth demonstrating that there's another side of the matplotlib api that is more specialized. To demonstrate this we'll now use the plt.subplots function (not plt.subplot) which gives us a more aligned way of plotting.

import numpy as np
import matplotlib.pylab as plt

def grid_plots(n, m):
  fig, axes = plt.subplots(n, m, figsize=(7, 7), sharex=True, sharey=True)
  for i, ax in enumerate(axes.flat):
      x = np.random.normal(i, 1, (1000,))
      ax.hist(x, bins=30)
      ax.set_title(f"$\mu$={i}, $\sigma$={1}")

grid_plots(3, 3)

If you're interested in all the code from these videos you can find the notebook on github.