Python, Seaborn FacetGrid change titles

前端 未结 2 1967
轮回少年
轮回少年 2020-12-17 15:27

I am trying to create a FacetGrid in Seaborn

My code is currently:

g = sns.FacetGrid(df_reduced, col=\"ActualExternal\", margin_titles=True)
bins = n         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 15:51

    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.

提交回复
热议问题