0

I am trying to keep all iterations of this loop on the same subplot figure. For example, here is the plotting part of my code (omitting how I obtain data).

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

As I iterate through this loop. I cannot figure out how to keep all iterations (6-9) on the same subplot figure. Currently, my code just creates new separate subplots and figures for each iteration as I loop through. I greatly appreciate any help.

  • How many axis would you like to have on the same figure, 4 or 4*4=16? – Yulia V Jul 22 '21 at 15:02
  • If you want a different `suptitle` for each value of `N`, I cannot see how you could avoid having 4 diffferent figures, each containing a 2×2 grid of subplots. I GUESS that you want a single figure, with a 2×2 grid, and each subplot contains 4 curves for different values of `N`, individuated by means of an appropriate label in the subplot legend. – gboffi Jul 22 '21 at 15:50
  • Also, from what you deigned to show us, you are plotting again and again the same stuff in the four iterations on `N`, the only things that depends on `N` is the `suptitle`! We could help better if you could try a little harder to explain your problem, thank you. – gboffi Jul 22 '21 at 15:55

1 Answers1

0

You want to create one figure with four subplots, don't instantiate the figure inside the loop:

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...
Jody Klymak
  • 4,979
  • 2
  • 15
  • 31