0

I have a dataframe with 4 columns and I want to plot each two columns in combination as line plots. The columns are independent of each other. But I want to plot them pairwise. This would eventually give me an n*n grid of subplots, which looks like this 4x4 gridplot using matplotlib subplot

This was generated using the following code. Since I can not upload the original df, I have created a fake one here.

Number_1 = np.random.uniform(0.5, 0, 30)
Number_2 = np.random.uniform(0.5, 0.25, 30)
Number_3 = np.random.uniform(-0.25, 0.25, 30)
Number_4 = np.random.uniform(0.25, 0, 30)

columns = ['Number_1','Number_2','Number_3','Number_4']
loadings = pd.DataFrame([Number_1,Number_2,Number_3,Number_4]).T
loadings.columns = columns


fig, axs = plt.subplots(loadings.shape[1], loadings.shape[1],figsize=(20,12),tight_layout=True)
fig.suptitle('Loading diagram for each component', fontsize=25)

for i in range(axs.shape[0]):
    for j in range(axs.shape[1]):
        if i!=j:
            axs[i,j].plot(loadings[columns[i]],label=columns[i])
            axs[i,j].plot(loadings[columns[j]],label=columns[j])
            axs[i,j].legend()
            axs[i,j].set_xlabel('x axis title')
        else:
            axs[i,j].axis('off')

plt.show()

But what I could not figure out was how I could add an outer common axis title for each row and column of the grid. What I want would eventually look like this desired result. Notice the Number_1, Number_2 etc. titles on the outer axes.

One thing that I found on matplotlib was using subfigures. But I could not figure out (pun intended) how to create both horizontal and vertical figures and then name them. I would really appreciate the help. Thank you very much!

dannyboy
  • 11
  • 4
  • Does this helps? https://stackoverflow.com/questions/25812255/row-and-column-headers-in-matplotlibs-subplots – Davide_sd Apr 22 '22 at 14:10
  • If you don't have an answer or if you tried the example comment and it didn't work, try this. `for k,c in enumerate(columns):axs[3,k].text(0.08, 0.2*k+0.15, columns[k], rotation=90, transform=fig.transFigure, font={'size':18});axs[k,3].text(0.2*k+0.18, 0.07, columns[k], transform=fig.transFigure, font={'size':18})` – r-beginners Apr 22 '22 at 14:14
  • @Davide_sd It is not that I did not look on stack overflow for a solution first. And yet I did not find the post which you posted. I wonder what am I doing wrong? Anyway, this was extremely helpful and resolved my issue. Thanks a bunch. – dannyboy Apr 25 '22 at 06:08
  • @r-beginners appreciate your help as well. Thanks. – dannyboy Apr 25 '22 at 06:08
  • This problem can be resolved with the link that @Davide_sd posted. https://stackoverflow.com/questions/25812255/row-and-column-headers-in-matplotlibs-subplots – dannyboy Apr 25 '22 at 06:09

0 Answers0