14

I just can't figure out how to change the xlabels in a Seaborn FacetGrid. It offers a method for changing the x labels with set_xlabels() but unfortunately not individually for each subplot.

I have two subplots which share the y-axis but have a different x-axes and I want to label them with different texts.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
J-H
  • 1,795
  • 6
  • 22
  • 41
  • As per [`seaborn.FacetGrid`](https://seaborn.pydata.org/generated/seaborn.FacetGrid.html), it's better to directly use [figure-level](https://seaborn.pydata.org/tutorial/function_overview.html#figure-level-vs-axes-level-functions) functions like [`g = sns.relplot(data=tips, kind='scatter', ...)`](https://seaborn.pydata.org/generated/seaborn.relplot.html) instead of `sns.FacetGrid`. The answers still work for this. – Trenton McKinney Apr 26 '23 at 12:54

2 Answers2

38

You can access the individual axes of the FacetGrid using the axes property, and then use set_xlabel() on each of them. For example:

import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style="ticks", color_codes=True)

tips = sns.load_dataset("tips")

g = sns.FacetGrid(tips, col="time",  hue="smoker")
g = g.map(plt.scatter, "total_bill", "tip", edgecolor="w")

g.axes[0,0].set_xlabel('axes label 1')
g.axes[0,1].set_xlabel('axes label 2')

plt.show()

enter image description here

Note in this example, g.axes has a shape of (1,2) (one row, two columns).

tmdavison
  • 64,360
  • 12
  • 187
  • 165
0

for all axis to set them once use this

g.set_axis_labels("Total bill ($)")
user690069
  • 330
  • 4
  • 13
  • 1
    The question is about individual axes labels for each subplot, therefore, your answer is not working (I made the same mistake when I read the question), – Dr. Manuel Kuehner Mar 06 '22 at 20:15