0

I'm having a hard time renaming the subplots axis in a Seaborn.PairGrid plot. This one was the main post I was following to try to solve my issue but for me it simply does not work. I have also tried with df.axes[i,j].set(ylabel='foo', xlabel='bar') as some other post suggested but it also does not work with the inner subplots, they still don't display the axis labels. The result I want is just like what tmdavison showed but I believe things might have changed slightly over the years due to package updates (since his answer is 6yo+).

This issue I'm having is very similar to this one reported here.

I even tried increasing the space between the subplots and ensuring both axis were enable but that also didn't make any difference.

for ax in g.axes.flat:
  ax.tick_params(axis='both', labelleft=True, labelbottom=True)

plt.subplots_adjust(wspace=0.7, hspace=0.3)

My full code based on the other post I mentioned:

import pandas as pd
import numpy as np    
import seaborn as sns
import matplotlib.pyplot as plt

iris = sns.load_dataset("iris")

g = sns.PairGrid(iris)
g = g.map(plt.scatter)

for ax in g.axes.flat:
  ax.tick_params(axis='both', labelleft=True, labelbottom=True)

plt.subplots_adjust(wspace=0.7, hspace=0.3)

xlabels,ylabels = [],[]

for ax in g.axes[-1,:]:
  xlabel = ax.xaxis.get_label_text()
  xlabels.append(xlabel)
for ax in g.axes[:,0]:
  ylabel = ax.yaxis.get_label_text()
  ylabels.append(ylabel)

for i in range(len(xlabels)):
  for j in range(len(ylabels)):
    g.axes[j,i].xaxis.set_label_text(xlabels[i])
    g.axes[j,i].yaxis.set_label_text(ylabels[j])

plt.show()

Result:

Running the above code gives me:

Versions I'm running: Python: 3.10.1; Seaborn: 0.12.0; Matplotlib: 3.6.1

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
luzclasil
  • 73
  • 4

1 Answers1

1

I was able to solve the problem by posting this issue at Seaborn's GitHub page and I wanted to post here for future reference. Indirectly quoting the dev in his reply, visible=True is the part this code is missing. So by simply changing

g.axes[j,i].xaxis.set_label_text(xlabels[i], visible=True)
g.axes[j,i].yaxis.set_label_text(ylabels[j], visible=True)

The above code should work as intended! s

luzclasil
  • 73
  • 4