Skip to content

Instantly share code, notes, and snippets.

@DanielTakeshi
Last active June 16, 2021 21:56
Show Gist options
  • Save DanielTakeshi/a4a8c431bd3ab30ed578f3b579083c7a to your computer and use it in GitHub Desktop.
Save DanielTakeshi/a4a8c431bd3ab30ed578f3b579083c7a to your computer and use it in GitHub Desktop.
Making subplots in matplotlib, then getting labels to appear at the bottom
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.style.use('seaborn')
import numpy as np
nrows, ncols = 1, 2
fig, ax = plt.subplots(nrows, ncols, sharey=False, squeeze=True, figsize=(9*ncols, 6*nrows))
x = np.arange(100)
y0 = x + np.random.normal(loc=0.0, scale=10.0, size=100)
y1 = x + np.random.normal(loc=0.0, scale=10.0, size=100)
y2 = x + np.random.normal(loc=0.0, scale=5.0, size=100)
ax[0].plot(x, y2, ls='-', lw=4 ,color='blue')
l1, = ax[0].plot(x, y0, ls='-', lw=4 ,color='red')
l2, = ax[1].plot(x, y1, ls='-', lw=4, color='blue')
# Note: not sure why we need the comma here ...
fig.legend(
handles=[l1, l2],
labels=['Red Data', 'Blue Data'],
loc='lower left',
prop={'size': 30},
bbox_to_anchor=(0.30, 0.0),
ncol=2,
shadow=True,
frameon=True,
)
plt.tight_layout()
plt.subplots_adjust(bottom=0.20)
figname = f'plot_testpng'
plt.savefig(figname)
@DanielTakeshi
Copy link
Author

plot_testpng

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