How to add labels to a pairplot

3.5k Views Asked by At

I have a dataframe with the following form:

          0         1         2         3  ...         9        10       11     output
0  2.451775  1.9565675  0.843128 -0.007820  ...  0.74554812  0.090777 -1.90625       a
1 -0.855458 -0.8444604 -0.619685  0.2273399  ... 1.3771857   4.089319 -0.16289       a
2  1.554580  1.9164567  0.643128 -0.0077550  ... 0.7771542   5.090777 -1.90625       b

which was the result of normalizing a numpy´s array and adding the last column named output. I add the last column to be able to use it in a sns.pairplot with the hue=output.

The problem that I have is that my pairplot is displayed with numbers like this: enter image description here

so I what I want is to display instead of numbers 0,1,2,..,n a set of labels, for that reason I have a list of labels like:

labels=["label 1","label 2",...,"label n" ]

that I want to add into my pairplot to have the labels names instead of the index numbers 0, 1,...n-1. I have done the following:

sns.pairplot(df,vars=labels,hue="output")

but I got the following error:

KeyError: 'label 1'

I have tried the option of `reset_index(drop=True), but no results at all. How can I fix this?

2

There are 2 best solutions below

0
On

I think you can do this by getting the information from the 'matplotlib' axis and setting it.

labels=["label 1","label 2",...,"label n" ]
g = sns.pairplot(df,vars=labels,hue="output")

for ax, lbl in zip(g.axes.flatten(), labels):
    # print(ax, lbl)
    ax.set_ylabel(f'{lbl}')
0
On

Either rename your dataframe columns or rename only the axis labels in the plot. For me the second option worked using ._add_axis_labels(). Try this:

labels=["label 1","label 2",...,"label n" ]
pairplot = sns.pairplot(df)
pairplot.x_vars = labels
pairplot.y_vars = labels
pairplot._add_axis_labels()