Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save asmwarrior/43c2bbd85ea12836f3b7038367b4a5c5 to your computer and use it in GitHub Desktop.
Save asmwarrior/43c2bbd85ea12836f3b7038367b4a5c5 to your computer and use it in GitHub Desktop.
matplotlib - Add subplots dynamically

Useful tip from the late creator of matplotlib, John Hunter.

http://matplotlib.1069221.n5.nabble.com/dynamically-add-subplots-to-figure-td23571.html

import matplotlib.pyplot as plt

# start with one
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3])

# now later you get a new subplot; change the geometry of the existing
n = len(fig.axes)
for i in range(n):
    fig.axes[i].change_geometry(n+1, 1, i+1)

# add the new
ax = fig.add_subplot(n+1, 1, n+1)
ax.plot([4,5,6])

plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment