Home > Back-end >  How to show subplots for each row
How to show subplots for each row

Time:07-21

Currently having an issue getting all of my data to show on my subplots. I'm trying to plot a 7 row 6 column subplot using geodataframes. This is what one of the geodataframes looks like (they all look the same).

My data is below:

# what I want to label the y axis for each row
ylab = ['mean_ensemble',
'mean_disalexi',
'mean_eemetric',
'mean_geesebal',
'mean_ptjpl',
'mean_ssebop',
'mean_sims']

# the years I want to plot and what the name of each column in the geodataframes are
years = ['2016', '2017', '2018', '2019', '2020', '2021']

# the 7 geodataframes
graph = [mean_ensemble,
mean_disalexi,
mean_eemetric,
mean_geesebal,
mean_ptjpl,
mean_ssebop,
mean_sims]

f, ax = plt.subplots(nrows = 7, ncols = 6, figsize = (12, 12))

ax = ax.flatten()

i=0

for y, col in enumerate(years):
    graph[i].plot(column=col, ax=ax[y], legend=True, cmap='Blues')
    ax[y].axis('off')
    plt.title(str(y))
    i =1

plt.show()


This is what I end up with.

I also want a title for the overall subplot that says "Mean ET Data for SD-6 Area". I'm not sure if I'm missing anything so any help would be appreciated.

CodePudding user response:

I think you need another loop to go through rows as well as columns. Hard to replicate exactly without your data sets, but I'd suggest something like this:

f, axs = plt.subplots(nrows=7, ncols=6, figsize = (12, 12))

for i in range(7):
    for j in range(6):
        graph[i].plot(column=years[j], ax=axs[i,j], legend=True, cmap='Blues')
        axs[i,j].axis('off')
        if i == 0:
            axs[i,j].set_title(ylab[j])

CodePudding user response:

I played around with @falafelocelot data and it worked! This was my final code:

# Create lists that the figure can iterate through when plotting
years = ['2016', '2017', '2018', '2019', '2020', '2021']
mean_et_list = [mean_ensemble, mean_disalexi, mean_eemetric, mean_geesebal, mean_ptjpl, mean_ssebop, mean_sims] # separated geodataframes
ylab = ['Ensemble', 'DisALEXI', 'eeMetric', 'geeSEBAL', 'PT-JPL', 'SSEBop', 'SIMS']

# Create figure and set axes
fig, axs = plt.subplots(nrows = 7, ncols = 6, figsize = (12, 12))

# Create for loop to iterate through rows and columns
rows = 7
cols = 6
for r in range(rows):
    for c in range(cols):
        mean_et_list[r].plot(years[c], ax=axs[r,c], legend=True, cmap='Blues')
        boundary.plot(ax=axs[r,c], edgecolor='black', color='white', alpha=0.3)
        buffer.plot(ax=axs[r,c], edgecolor='black', color='white', alpha=0.3)
        axs[r,c].patch.set_facecolor('white')
        axs[r,c].set_xticks([]) # remove tick marks 
        axs[r,c].set_xticklabels([]) # remove tick mark labels
        axs[r,c].set_yticks([])
        axs[r,c].set_yticklabels([])
        axs[r,c].spines['top'].set_visible(False) # remove border around plot
        axs[r,c].spines['right'].set_visible(False)
        axs[r,c].spines['bottom'].set_visible(False)
        axs[r,c].spines['left'].set_visible(False)
        if c == 0:
            axs[r,c].set_ylabel(ylab[r]) # set y-labels for first column
        if r == 0:
            axs[r,c].set_title(years[c]) # set year titles for each column

fig.suptitle('Annual Mean ET for SD-6', y=0.93)
fig.patch.set_facecolor('white')
os.chdir('C:\\Users\\ashle\\Desktop\\KGS\eggs\\')
plt.savefig('AnnualMeanETSD6.png')
plt.show()

This was my final plot

  • Related