Sofija Yanez
Sofija Yanez

Reputation: 25

Make subplots using a for loop

I am trying to plot 6 graphs to a file using matplotlib subplots. I managed to do this but my code was very repetitive (see below), so I am trying to change the code to fit a for loop. My x axis is the same for all plots, and I want to iterate through i,j of the zipped list of y values and labels, and plot the 6 subplots.

p1 = [p1_atm, p1_bio, p1_soil, p1_so, p1_do, p1_geo]
labels = ["Atmosphere", "Biosphere", "Soils", "Shallow Ocean", "Deep Ocean", "Geosphere"]
p1_labels = zip(p1, labels)
#%% 
filename1 = 'no_emissions_pools.png'

fig, axs = plt.subplots(6,1, figsize=(8,6))
for i,j in list(p1_labels):
    k = 0
    axs[k].plot(t_2020, i, color = '#6d9eeb', label = j)
    axs[k].set(xlim=[min(t_2020),max(t_2020)+1], ylim=[min(i), max(i)], ylabel = 'PgC')
    axs[k].legend()
    k = k + 1
    
fig.tight_layout()
plt.savefig(filename1,bbox_inches='tight')
plt.show()
plt.close()

I am not getting any errors, I just get empty plots. Any ideas on how to solve this would be appreciated. Here is the original repetitive code that worked (colors are not important, and it doesn't matter if the graphs are plotted in a 3x2 or 6x1 shape):

fig, axs = plt.subplots(3, 2, figsize = (8,8))
axs[0, 0].plot(t, atm, color = '#6d9eeb', label = 'Atmosphere')
axs[0,0].set(xlim=[min(t),max(t)+1], ylim=[min(atm), max(atm)], ylabel = 'PgC')
axs[0,0].legend()

axs[0,1].plot(t, bio, color = '#93c47d', label = 'Biosphere')
axs[0,1].set(xlim=[min(t),max(t)+1], ylim=[min(bio), max(bio)])
axs[0,1].legend()
                    
axs[1,0].plot(t, soil, color = '#dd7e6b', label = 'Soil')
axs[1,0].set(xlim=[min(t),max(t)+1], ylim=[min(soil), max(soil)],ylabel = 'PCg')
axs[1,0].legend()
    
axs[1,1].plot(t, so, color = '#9fc5e8', label = 'Shallow Ocean')
axs[1,1].set(xlim=[min(t),max(t)+1], ylim=[min(so), max(so)])
axs[1,1].legend()

axs[2,0].plot(t, do, color = '#45818e', label = 'Deep Ocean')
axs[2,0].set(xlim=[min(t),max(t)+1], ylim=[min(do), max(do)], xlabel = 'Year', ylabel = 'PCg')
axs[2,0].legend()

axs[2,1].plot(t, geo, color = '#906861', label = 'Geosphere')
axs[2,1].set(xlim=[min(t),max(t)+1],ylim=[min(geo), max(geo)], xlabel = 'Year')
axs[2,1].legend()
    
fig.tight_layout()
plt.savefig(filename,bbox_inches='tight')
plt.show()
plt.close()

Upvotes: 1

Views: 353

Answers (1)

Sofija Yanez
Sofija Yanez

Reputation: 25

I found a solution in case anyone else ever wants to reference this. So basically the axs.flatten works by making the axs iterable, so the k counter is not needed. Essentially I zipped all the variables that I would need for the loop including the axs, and the loop worked:

    for p,l,c,ax in zip(px,labels,colors,axs.flatten()):
        ax.plot(t, p, color = c, label = l)
        ax.set(xlim=[min(t),max(t)+1], ylim=[min(p), max(p)], ylabel = 'PgC')
        ax.legend()

    fig.tight_layout()
    plt.figtext(0.55,0,'Year') 
    plt.savefig(filename,bbox_inches='tight')
    plt.show()

Upvotes: 1

Related Questions