specify color in seaborn catplot

10,861

You can achieve the results you want by adding a boolean column to the dataframe and using it as the hue parameter of the catplot() call. This way you will obtain the results with two colours (one for pepe observations and one for the rest). Results can be seen here:

Also, the parameter legend=False should be set since otherwise the legend for is_pepe will appear on the side.

The code would be as below:

df['is_pepe'] = df['name'] == 'pepe'

ax  = sns.catplot(x='fav_color',
                  y='score',
                  col='country',
                  col_wrap=2,
                  data=df,
                  kind='swarm',
                  hue='is_pepe',
                  legend=False) 

Furthermore, you can specify the two colours you want for the two kinds of observations (pepe and not-pepe) using the parameter palette and the top-level function sns.color_palette() as shown below:

ax  = sns.catplot(x='fav_color',
                  y='score',
                  col='country',
                  col_wrap=2,
                  data=df,
                  kind='swarm',
                  hue='is_pepe',
                  legend=False,
                  palette=sns.color_palette(['green', 'blue']))

Obtaining this results:

Share:
10,861
Nabla
Author by

Nabla

Updated on June 04, 2022

Comments

  • Nabla
    Nabla almost 2 years

    I would like to specify the color of particular observations using seaborn catplot. In a made up exemple:

    import seaborn as sns
    import random as r
    
    name_list=['pepe','Fabrice','jim','Michael']
    country_list=['spain','France','uk','Uruguay']
    favourite_color=['green','blue','red','white']
    
    df=pd.DataFrame({'name':[r.choice(name_list) for n in range(100)],
                 'country':[r.choice(country_list) for n in range(100)],
                 'fav_color':[r.choice(favourite_color) for n in range(100)],
                 'score':np.random.rand(100),
                })
    
    sns.catplot(x='fav_color',
               y='score',
               col='country',
               col_wrap=2,
               data=df,
               kind='swarm')
    

    I would like to colour (or mark in another distinctive way, it could be the marker) all the observations with the name 'pepe'. How I could do that ? The other colors I dont mind, it would be better if they are all the same.

    catplot exemple

    • ImportanceOfBeingErnest
      ImportanceOfBeingErnest over 5 years
      Seaborn lacks a bit of flexibility here. So I would guess you cannot do it with a catplot. However using a FacetGrid and mapping customized swarmplots to it might work. I.e. using a function that plots two swarmplots per plot, one with all data and another one with only the "pepe" data.