Title: Automate the Populating of Subplots with different data
Tags: python;matplotlib
Question: I am trying to do the following:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 400)
y1 = np.sin(x)
y2 = np.sin(x**2)
y3 = np.cos(x**2)
#plot
f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)
ax1.plot(x, y1)
ax1.set_title('lalalala')
ax2.plot(x, y2)
ax3.plot(x, y3)
f.subplots_adjust(hspace=0)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
Instead of entering ax1 to ax3 and y1 to y3 manually, I was wondering if there was any way to loop over the ax's and y's to populate the plot. I am aware of this post but the best answer that is given there does not loop over the data. Thanks!

Here is the accepted answer: Sure, just put in a list all you want and add subplots in a loop:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 400)
y = [np.sin(x), np.sin(x**2), np.cos(x**2)]
title = ['y1','y2','y3']
#plot
numPlots = len(y)
f = plt.figure()
ax = []
for i in range(numPlots):
ax.append(f.add_subplot(numPlots,1,i+1))
ax[i].plot(x, y[i])
ax[i].set_title(title[i])
f.subplots_adjust(hspace=0.3)
plt.setp([a.get_xticklabels() for a in f.axes[:-1]], visible=False)
plt.show()


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:09/16/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><matplotlib>', 'Title': 'Automate the Populating of Subplots with different data', 'ViewCount': 51, 'id': 32613108, 'user_AboutMe': '<p>noob</p>\n', 'user_AccountId': 6633153.0, 'user_DisplayName': 'gjy', 'user_Reputation': 39.0, 'user_Views': 25.0, 'user_id': 5167241.0}}