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:
Versions I'm running: Python: 3.10.1; Seaborn: 0.12.0; Matplotlib: 3.6.1