为什么子图不在一个图形上保持迭代?

Why does subplot not keep iterations on one figure?

我正在尝试将此循环的所有迭代保留在同一个子图中。例如,这是我的代码的绘图部分(省略了我如何获取数据)。

for N in range(6,10):

    fig,axs = plt.subplots(2, 2 , sharex=True)
    
    axs[0, 0].plot(t1, xnew[0::4])
    axs[0, 0].set_title('Minimize Position')
    axs[0, 0].legend(["Position"],loc='best')


    axs[0, 1].plot(t1, xnew[1::4], 'tab:orange')
    axs[0, 1].set_title('Minimize Radians')
    axs[0, 1].legend(["Radians"],loc='best')


    axs[1, 0].plot(rk4_inter.t,rk4_inter.y[1], 'tab:green')
    axs[1, 0].set_title('RK4 Position')
    axs[1, 0].legend(["Position"],loc='best')


    axs[1, 1].plot(rk4_inter.t,rk4_inter.y[2], 'tab:red')
    axs[1, 1].set_title('RK4 Radians')
    axs[1, 1].legend(["Radians"],loc='best')
    fig.suptitle('Minimize and RK4 Solutionslabel % s collocations' % N, fontsize=14)
    fig.tight_layout()
 
plt.show()

当我遍历这个循环时。我无法弄清楚如何将所有迭代 (6-9) 保留在同一个子图上。目前,我的代码只是在我循环时为每次迭代创建新的单独的子图和图形。我非常感谢任何帮助。

你想创建一个有四个子图的图形,不要在循环中实例化图形:

fig,axs = plt.subplots(2, 2 , sharex=True)
for N in range(6,10):
    ax = axs.flat[N-6]
    ax.plot(x, y[N])  # or whatever you wanted to plot here...