pythonseabornsubplotfacet-gridcatplot

Make Left and Bottom FacetGrid Spines Visible


I try to come up with a helper function to plot figure with subplots in Seaborn.

The codes currently look like below:

def granular_barplot(data, col_name, separator):
    '''
    data = dataframe
    col_name: the column to be analysed
    separator: column to be plotted in subplot
    '''
    g = sns.catplot(data=data, y=col_name, col=separator, kind='count',color=blue)
    g.fig.set_size_inches(16,8)
    g.fig.suptitle(f'{col_name.capitalize()} Changes by {separator.capitalize()}',fontsize=16, fontweight='bold')
    g.despine()
    
    for ax in g.axes.ravel():
        for c in ax.containers:
            ax.bar_label(c)

and it produces the graph like this:

enter image description here

What I'm trying to achieve is to make the left and bottom spines visible for each subplots in the helper function like below (which is similar to the sns.despine function): enter image description here


Solution

  • Try setting this style:

    def granular_barplot(data, col_name, separator):
        '''
        data = dataframe
        col_name: the column to be analysed
        separator: column to be plotted in subplot
        '''
        sns.set_style({'axes.linewidth': 2, 'axes.edgecolor':'black'})
        g = sns.catplot(data=data, y=col_name, col=separator, kind='count',color='blue')
        g.fig.set_size_inches(16,8)
        g.fig.suptitle(f'{col_name.capitalize()} Changes by {separator.capitalize()}',fontsize=16, fontweight='bold')
        g.despine()
        
        for ax in g.axes.ravel():
            ax.spines['left'].set_visible(True)
            ax.spines['bottom'].set_visible(True)
    
    df = sns.load_dataset('tips')
    granular_barplot(df, 'sex', 'smoker')
    

    Output:

    enter image description here