Skip to content

Instantly share code, notes, and snippets.

@selwyth
Last active August 29, 2015 14:21
Show Gist options
  • Save selwyth/40ad105d2e9908c4f83a to your computer and use it in GitHub Desktop.
Save selwyth/40ad105d2e9908c4f83a to your computer and use it in GitHub Desktop.
Subplots
# source: http://matplotlib.org/examples/pylab_examples/demo_tight_layout.html
# define initially
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex='col', sharey='row')
# MATLAB numbering
fig = plt.figure()
ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(223)
ax4 = plt.subplot(224)
# add_subplot
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)
plt.gca() # get current axis
plt.gcf() # get current figure
# using gridspec to customize subplot dimensions
from matplotlib import gridspec
gs = gridspec.GridSpec(7,1, height_ratios = [1,1,1,5,1,1,1])
fig = plt.figure(figsize=(12,12))
for i, ss in zip(df.PLATFORM.unique(), gs):
ax = fig.add_subplot(ss)
df2[df2.PLATFORM==i].plot(kind='barh', ax=ax)
ax.set_ylabel(i)
ax.tick_params(labelsize=7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment