Last active
June 16, 2021 21:56
-
-
Save DanielTakeshi/a4a8c431bd3ab30ed578f3b579083c7a to your computer and use it in GitHub Desktop.
Making subplots in matplotlib, then getting labels to appear at the bottom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Author
DanielTakeshi
commented
Jun 16, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment