0

This question was closed just after I asked it, as it was deemed to be a duplicate. However, the suggested duplicate : How to plot in multiple subplots does not address my question, which is how to plot subplots in a subplot, i.e 'a number of subplots, each of which is made up of two (or more) subplots'.

I have this code, which plots individual plots (each with two subplots) the number of plots is determined by the length of ev_ind

for ij in range(len(ev_ind)):
            dfn=str('event_'+ev_ind.loc[ij,0].astype(str))
            locals()[dfn]=pd.DataFrame(columns=['datetime', 'aaj', 'cbd', 'cbu','dmr', 'fms'])
            edfn=str('event_'+ev_ind.loc[ij,0].astype(str)+'_rain')
            locals()[edfn]=pd.read_csv(edname+dfn+'_edf.csv', usecols=[1,2,3,4,5,6,7,8,9,10,11])
            for xx in gl2:
                locals()[dfn]['datetime']=pd.read_csv(edname+dfn+'_'+xx+'.csv', usecols=['datetime'])
                locals()[dfn][xx]=pd.read_csv(edname+dfn+'_'+xx+'.csv', usecols=['depth'])
            locals()[dfn]['datetime']=locals()[dfn]['datetime'].astype('datetime64[ns]')
            locals()[edfn]['datetime']=locals()[edfn]['datetime'].astype('datetime64[ns]')
            sd=locals()[dfn]['datetime'].iloc[0]
            ed=locals()[dfn]['datetime'].iloc[-1]
            sds=sd.strftime('%b %d %H:%M')
            eds=ed.strftime('%b %d %H:%M')
            subfig=fig.subfigures(row+1,col+1)
            #axs[row,col],(ax1, ax2)= fig.subfigures()
            #axs[row,col], (ax1, ax2)=plt.subplots(2,1, sharex=True)
            #axs.suptitle(f'Cluster mean and river levels for {gname} event {ev_ind.loc[ij,0]} in period {sds} to {eds}')
            #subfigij, (ax1, ax2)=subfig.subplots(2,1, sharex=True) 
            figij, (ax1, ax2)=plt.subplots(2,1, sharex=True)
            figij.suptitle(f'Cluster mean and river levels for {gname} event {ev_ind.loc[ij,0]} in period {sds} to {eds}')
            for xx in gl2:
                 ax1.plot(locals()[dfn]['datetime'], locals()[dfn][xx]*100, label=gl[gl2.index(xx)], color= colour2[gl2.index(xx)])
            ax1.title.set_text('stream level')
            ax1.set_ylabel('stream depth (cm)', rotation=90)
            ax1.legend(title='Stream name',bbox_to_anchor=(1,1), loc="upper left", fontsize=10, title_fontsize=12)
            ax1.xaxis.set_minor_formatter(plt.NullFormatter())
            
            ax2.plot(locals()[edfn].datetime, locals()[edfn].ic_mean, label='ic mean', color= 'red')
            ax2.plot(locals()[edfn].datetime, locals()[edfn].mc_mean, label='mc mean', color= 'chocolate')
            ax2.plot(locals()[edfn].datetime, locals()[edfn].imd_mean, label='imd mean', color= 'orange')
            ax2.plot(locals()[edfn].datetime, locals()[edfn].md_mean, label='md mean', color='navy')
            ax2.plot(locals()[edfn].datetime, locals()[edfn].oak_mean, label='oak mean', color= 'purple')
            ax2.plot(locals()[edfn].datetime, locals()[edfn].iloc[:,di], label= gname, color='black')
            ax2.title.set_text('Guage readings')
            ax2.legend(title='gauge name',bbox_to_anchor=(1,1), loc="upper left",  fontsize=10, title_fontsize=12)
            ax2.set_ylabel('Rain (mm)', rotation=90)
            ax2.set_xlabel('Datetime')
            ax2.tick_params(axis='x',rotation=90)
            plt.subplots_adjust(right=0.7)
           
            plt.show()

what I would like to do is plot each of these plots on one plot. I have tried all sorts of different things, as you can see by the commented out lines, but cannot get anything to work!

matt cooper
  • 101
  • 1
  • 8
  • Please give a minimal reproducible set of code; you may want to look at `subplot_mosaic` and `subfigures`. https://matplotlib.org/stable/tutorials/provisional/mosaic.html – Jody Klymak Aug 13 '22 at 01:27
  • @JodyKlymak I cant give 'reproducible set of code' as it would entail uploading many dataframes, which stack overflow won't let me do. the question is very simple. Each subplot contains two subplots, how do I do this in a loop? – matt cooper Aug 15 '22 at 11:02
  • Numpy and pandas have simple ways to make random synthetic datasets. – Jody Klymak Aug 15 '22 at 13:29
  • As usual, stack overflow is more bothered about the format of the question than answering the question. I figured it out eventually. Without your help. – matt cooper Aug 15 '22 at 15:03
  • a) I gave you a link above; b) If you give us example code we can edit, rather than your undigested, irreproducible code, we can actually answer the question in a way that will help others; c) if you take the time to make a reproducible example, you can often find the answer yourself. https://stackoverflow.com/help/how-to-ask – Jody Klymak Aug 15 '22 at 15:15

0 Answers0