matplotlib twinx inside a loop

前端 未结 1 443
鱼传尺愫
鱼传尺愫 2021-01-24 12:29

I have a loop which for a certain condition (occurs e.g 3 times), I\'d like to plot two dataset with twinx, so e.g at the end I have 3 plots on left-y and 3 plots on right-y. Wh

相关标签:
1条回答
  • 2021-01-24 12:32

    You need to move your "ax2" definition outside the loop (see below). However, this makes seven plots, four on left, three on right. You need to update your "if" condition.

    [![import numpy as np
    import matplotlib.pyplot as plt
    
    x=np.linspace(0,10,100)
    A=5
    
    fig,ax=plt.subplots()
    ax2=ax.twinx()
    
    for i in range(0,10):
      A=i**2
      if(i%3==0):
        ax.plot(x,A*np.sin(x),'-ro')
        ax2.plot(x,A*x,'-bp')
    plt.show()][1]][1]
    

    0 讨论(0)
提交回复
热议问题