Skip to content Skip to sidebar Skip to footer

Python, Seaborn FacetGrid Change Titles

I am trying to create a FacetGrid in Seaborn My code is currently: g = sns.FacetGrid(df_reduced, col='ActualExternal', margin_titles=True) bins = np.linspace(0, 100, 20) g.map(plt.

Solution 1:

Although you can iterate through the axes and set the titles individually using matplotlib commands, it is cleaner to use seaborn's built-in tools to control the title. For example:

# Add a column of appropriate labels
df_reduced['measure'] = df_reduced['ActualExternal'].replace({0: 'Internal',
                                                              1: 'External'}

g = sns.FacetGrid(df_reduced, col="measure", margin_titles=True)
g.map(plt.hist, "ActualDepth", color="steelblue", bins=bins, width=4.5)

# Adjust title and axis labels directly
g.set_titles("{col_name}")  # use this argument literally
g.set_axis_labels(x_var="Percentage Depth", y_var="Number of Defects")

This has the benefit of not needing modification regardless of whether you have 1D or 2D facets.


Solution 2:

You can access the axes of a FacetGrid (g = sns.FacetGrid(...)) via g.axes. With that you are free to use any matplotlib method you like to tweak the plot.

Change titles:

axes = g.axes.flatten()
axes[0].set_title("Internal")
axes[1].set_title("External")

Change labels:

axes = g.axes.flatten()
axes[0].set_ylabel("Number of Defects")
for ax in axes:
    ax.set_xlabel("Percentage Depth")

Note that I prefer those above the FacetGrid's internal g.set_axis_labels and set_titles methods, because it makes it more obvious which axes is to be labelled.


Solution 3:

The easiest way to set multiple titles would be:

titles = ['Internal','External']

for ax,title in zip(g.axes.flatten(),titles):
    ax.set_title(title )

Post a Comment for "Python, Seaborn FacetGrid Change Titles"