从 seaborn catplot 可视化中排除其中一种色调

exclude one of the hue from seaborn catplot visualization

我想通过 seaborn catplot 可视化类别计数,但其中一种色调并不重要,不需要包括可视化。 我如何 select catplot 中的特定色调进行可视化而不更改或删除列中的任何值?

您可以从数据框中删除具有该值的行。如果该列是 Categorical,您可能还需要更改类别,因为图例仍将包含所有类别。

这是一个例子:

import seaborn as sns
import pandas as pd

tips = sns.load_dataset('tips')
tips['day'].dtype # CategoricalDtype(categories=['Thur', 'Fri', 'Sat', 'Sun'], ordered=False)
# create a subset, a copy is needed to be able to change the categorical column
tips_weekend = tips[tips['day'].isin(['Sat', 'Sun'])].copy()
tips_weekend['day'].dtype # CategoricalDtype(categories=['Thur', 'Fri', 'Sat', 'Sun'], ordered=False)
tips_weekend['day'] = pd.Categorical(tips_weekend['day'], ['Sat', 'Sun'])
tips_weekend['day'].dtype # CategoricalDtype(categories=['Sat', 'Sun'], ordered=False)
sns.catplot(data=tips_weekend, x='smoker', y='tip', hue='day')

后续问题,一个histplotmultiple='fill'可以显示百分比分布:

import seaborn as sns
import pandas as pd
from matplotlib.ticker import PercentFormatter

tips = sns.load_dataset('tips')
tips_weekend = tips.copy()
tips_weekend['day'] = tips_weekend['day'].apply(lambda x: x if x in ['Sat', 'Sun'] else 'other')
# fix a new order
tips_weekend['day'] = pd.Categorical(tips_weekend['day'], ['other', 'Sat', 'Sun'])

ax = sns.histplot(data=tips_weekend, x='smoker', hue='day', stat='count', multiple='fill',
                  palette=['none', 'turquoise', 'crimson'])
# remove the first label ('other') in the legend
ax.legend(handles=ax.legend_.legendHandles[1:], labels=['Sat', 'Sun'], title='day')
ax.yaxis.set_major_formatter(PercentFormatter(1))
# add percentages
for bar_group in ax.containers[:-1]:
    ax.bar_label(bar_group, label_type='center', labels=[f'{bar.get_height() * 100:.1f} %' for bar in bar_group])