Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn PairGrid: show axes labels for each subplot

Is there a way that I can easily add the axes labels for each of the subplots in Seaborn pair plot? This is related to this question but instead of adding the tick labels I want to add the axes labels as the pairplot I am having is 9*9 and I dont want to scroll down every time to check the column name.

I was hoping that it would be some thing easy like

for ax in g.axes.flat:
    _ = plt.setp(ax.get_ylabels(), visible=True)
    _ = plt.setp(ax.get_xlabels(), visible=True)
like image 946
MARK Avatar asked Oct 27 '15 19:10

MARK


1 Answers

You first need to get all the labels from the axes (e.g. ax.xaxis.get_label_text()) and the set the label text (ax.xaxis.set_label_text()).

I've used a for loop and i, j indexing here. Its possible there's a cleaner vectorised way to do this, but at least it works.

Using the iris sample dataset from seaborn:

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)

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()

enter image description here

like image 91
tmdavison Avatar answered Sep 27 '22 23:09

tmdavison