Elizabeth
Elizabeth

Reputation: 241

python subploting in a loop, only the first subplot is shown

Hi I am experiencing subplotting in a loop. I failed to plot with using below code. The bottom subplot is empty

x=[1,2,3]
y=[4,5,6]
fig, ax = plt.subplots(2,1, sharex = True)

for n in range(2):
    ax[n].plot(x,y)
    plt.show()

the failed plot looks like this. Any thoughts? enter image description here

enter image description here

Upvotes: 0

Views: 534

Answers (2)

9769953
9769953

Reputation: 12192

Move plt.show() outside the for loop:

for n in range(2):
    ax[n].plot(x,y)
plt.show()

Upvotes: 1

Sanju Halder
Sanju Halder

Reputation: 96

Is this how you wanted it to be ??

x=[1,2,3]
y=[4,5,6]
fig, ax = plt.subplots(2,1,sharex = True)
for n in range(2):
    ax[n].plot(x,y)
plt.show()

output: enter image description here

Upvotes: 0

Related Questions