Search code examples
pythonmatplotlibseaborn

How to set labels on jointplot, pairplot, heatmap, catplot


I am drawing graphs using seaborn on python and want to label x axis and y axis.

When I draw only one graph such as barplot, rugplot, histogram, etc, I could just use:

pyplot.xlabel('x') pyplot.ylabel('y')

But for graphs like jointplot, pairplot, heatmap, and catplot, outcome is different from what I expected.

Jointplot draws scatterplot inside and histogram outside. So when I use pyplot.xlabel('x') pyplot.ylabel('y'), labels are drawn on axes of histogram.

This code draws 4 catplot and using xlabel, ylabel only labels the bottom catplot.

import seaborn as sns
from matplotlib import pyplot
tips = sns.load_dataset("tips")

sns.catplot(x="sex", y="total_bill", hue="smoker",row="day",
            kind="violin", data=tips, split=True)
pyplot.suptitle("tip - catplot", y=1.01)
pyplot.xlabel('x')
pyplot.ylabel('y')
pyplot.show();

How can I label the axes of the entire figure? or each of the graphs?


Solution

  • You can grab each axes object by first assigning the return of catplot to a variable. The axes are obtained using the axes attribute. Then set labels for these axes:

    g = sns.catplot(x="sex", y="total_bill", hue="smoker",row="day",
                kind="violin", data=tips, split=True)
    for ax, lbl in zip(g.axes.flatten(), 'abcd'):
        ax.set_xlabel(f'x axis {lbl}')