Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, Seaborn FacetGrid change titles

Tags:

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.hist, "ActualDepth", color="steelblue", bins=bins, width=4.5) 

This gives my the Figure

My FacetGrid

Now, instead of "ActualExternal =0.0" and "ActualExternal =1.0" I would like the titles "Internal" and "External"

And, instead of "ActualDepth" I would like the xlabel to say "Percentage Depth"

Finally, I would like to add a ylabel of "Number of Defects".

I've tried Googling and have tried a few things but so far no success. Please can you help me?

Thanks

like image 901
jlt199 Avatar asked May 11 '17 15:05

jlt199


People also ask

How do I add a title to FacetGrid?

If you create the FacetGrid directly, as in the original example, it automatically adds column and row labels instead of individual subplot titles. We can still add a title to the whole thing: from matplotlib. pyplot import scatter as plt_scatter g = sns.

How do you plot FacetGrid?

Plotting Small Multiples of Data SubsetsA FacetGrid can be drawn with up to three dimensions − row, col, and hue. The first two have obvious correspondence with the resulting array of axes; think of the hue variable as a third dimension along a depth axis, where different levels are plotted with different colors.

What is Col_wrap?

col_wrapint. “Wrap” the column variable at this width, so that the column facets span multiple rows. Incompatible with a row facet. share{x,y}bool, 'col', or 'row' optional. If true, the facets will share y axes across columns and/or x axes across rows.


1 Answers

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.

like image 103
jakevdp Avatar answered Sep 24 '22 12:09

jakevdp