seaborn FacetGrid 中的 hue 始终是数字的,带有 stripplot

hue always numerical in seaborn FacetGrid with stripplot

我不确定我做错了什么。我基本上有以下情况,我想将颜色变量解释为分类变量。 'a' 的不同值应该很容易通过颜色识别。

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')

我尝试在 FacetGrid 中定义 hue,但是 'a' 不再映射到 x 轴。

为了通过颜色轻松区分点,您可以使用 palette 参数明确设置颜色映射。

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