Search code examples
pythonseabornfacet-grid

hue always numerical in seaborn FacetGrid with stripplot


I'm not sure what I'm doing wrong. I basically have the below situation and I want to have the color variable interpreted as categorical. Different values of 'a' should be easily recognizable by color.

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame(dict(
    a=['a'] * 5 + ['b'] * 5,
    b=[1, 2, 3, 2, 1] * 2,
    c=['x', 'y'] * 5))
g = sns.FacetGrid(df, col='c', sharey=True)
g = g.map_dataframe(sns.stripplot, x='a', y='b', hue='a')

I tried defining hue in FacetGrid but then 'a' is not mapped to the x axis anymore.


Solution

  • To easily distinguish the dots by color, you can set the color map explicitly with the palette parameter.

    g = g.map_dataframe(sns.stripplot, x='a', y='b', hue='a', palette='tab10')
    

    enter image description here