0

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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
1cjtc jj
  • 77
  • 4
  • Instead of despining the entire thing, why not despine the top and right? [similar to this post here](https://stackoverflow.com/questions/22082111/how-to-despine-a-matplotlib-and-seaborn-axes) – Michael S. Jul 13 '22 at 15:49

2 Answers2

2

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

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
-1

You should either be able to pass some settings to seaborn's despine function or use matplotlib's ability to set spine visibility:

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(1,100)
y = np.arange(1,100)

g = sns.lineplot(x=x,y=y)
plt.title("Seaborn despine")
sns.despine(left=False, bottom=False)
plt.show()

Seaborn

g = sns.lineplot(x=x,y=y)
plt.title("False Spines")
g.spines['right'].set_visible(False)
g.spines['top'].set_visible(False)
plt.show()

Matplotlib

Michael S.
  • 3,050
  • 4
  • 19
  • 34