0

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()

enter image description here

1
  • for i, ax in zip(range(n), axes):, ax.hist(....). Commented Jun 11, 2020 at 21:02

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.