-1

Using the matplotlib library, I can create a grid of subplots that display data related to various combinations of illumination and window conditions with this code. You can see the code below. I would like to hold all the plots until last time loop iterates and basically I will have 9 subplots with 9 different data according to their illumination window combination.

This code successfully displays the plot in the correct position. However, the previous plots in the axes are cleared and only the current one is displayed. How can I hold all of the subplots when the loop iterates?

illumination_window_combin = ['light_window', 'light_nowindow', 'light_random', 'dimlight_window', 'dimlight_nowindow', 'dimlight_random', 'dark_window', 'dark_nowindow', 'dark_random']
count = 0
# Create a figure
fig, axs = plt.subplots(3, 3)

# Loop over the illumination conditions
for i, illumination in enumerate(illuminations):
    # Loop over the window conditions
    for j, window in enumerate(windows):
        if count < len(illumination_window_combin) and combination == illumination_window_combin[count]:
            for idx in range(fishPosAllCrop.shape[0]):
                # Check if the value of idx is less than the number of rows in axs
                if i < axs.shape[0] and j < axs.shape[1]:
                    # Plot the data on the current subplot
                    axs[i, j].plot(time, np.transpose(curdata['fishPosAllCrop'][idx]), label=f'Trial {idx + 1}')
                    axs[i, j].plot(time, np.transpose(curdata['shuttlePosAllCrop']), 'r', label='Shuttle')
                    axs[i, j].set_title(f'FishPosAllCrop - Illumination: {illuminations}, Window: {windows}')
                else:
                    continue
        count += 1
# Show the plots
plt.tight_layout()
plt.show()

I have tried using hold, pause and making other loops but none of them worked. (Apparently hold is not in use currently.)

jared
  • 4,165
  • 1
  • 8
  • 31
bskozkr
  • 1
  • 1
  • The question does not contain a complete [mre]. Please review #3 of [Creating Effective Stack Overflow Questions](https://trenton3983.github.io/files/Creating_Effective_StackOverflow_Questions.html). – Trenton McKinney Aug 31 '23 at 15:17
  • In any case, it seems like you may be overwriting subplots in this implementation. Please review this [answer](https://stackoverflow.com/a/68793513/7758804) to more succinctly plot on your subplots from pandas. – Trenton McKinney Aug 31 '23 at 15:19

0 Answers0