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

Time:08-02

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

CodePudding user response:

Move plt.show() outside the for loop:

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

CodePudding user response:

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

  • Related