Reputation: 321
I am trying to place multiple histograms in a vertical stack. I am able to get the plots in a stack but then all the histograms are in the same graph.
fig, ax = plt.subplots(2, 1, sharex=True, figsize=(20, 18))
n = 2
axes = ax.flatten()
for i, j in zip(range(n), axes):
plt.hist(np.array(dates[i]), bins=20, alpha=0.3)
plt.show()
Upvotes: 0
Views: 375
Reputation: 39052
You have a 2x1 grid of axis objects. By directly looping over axes.flatten()
, you are accessing one subplot at a time. You then need to use the corresponding axis instance for plotting the histogram.
fig, axes = plt.subplots(2, 1, sharex=True, figsize=(20, 18)) # axes here
n = 2
for i, ax in zip(range(n), axes.flatten()): # <--- flatten directly in the zip
ax.hist(np.array(dates[i]), bins=20, alpha=0.3)
Your original version was also correct except the fact that instead of plt
, you just should have used the correct variable i.e. j
instead of plt
for i, j in zip(range(n), axes):
j.hist(np.array(dates[i]), bins=20, alpha=0.3)
Upvotes: 1