matplotlib 向海运多箱线图添加影线[重复]

7kjnsjlb  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(126)

此问题在此处已有答案

How to add hatches to boxplots with sns.boxplot or sns.catplot(1个答案)
2天前关闭。
在下面的图表中,我想添加影线(“/”)只到“休息”类别中的两个boxlot。我会很高兴,如果你的帮助
我添加了以下示例代码:

import seaborn as sns
exercise = sns.load_dataset("exercise")

df1=exercise.loc[(exercise["diet"]=="low fat"),:]
df2=exercise.loc[(exercise["diet"]=="no fat"),:]

fig, axes = plt.subplots(1, 2)
ax1=sns.boxplot(x='kind', y='pulse', orient='v', ax=axes[0],data=df1, showfliers=False)
ax2= sns.boxplot(x='kind', y='pulse', orient='v', ax=axes[1],data=df2, showfliers=False)

rsl1atfo

rsl1atfo1#

您可以迭代xticks标签和面片,并仅为rest设置影线:

import seaborn as sns
import matplotlib.pyplot as plt

exercise = sns.load_dataset("exercise")

df1=exercise.loc[(exercise["diet"]=="low fat"),:]
df2=exercise.loc[(exercise["diet"]=="no fat"),:]

fig, axes = plt.subplots(1, 2)
sns.boxplot(x='kind', y='pulse', orient='v', ax=axes[0],data=df1, showfliers=False)
sns.boxplot(x='kind', y='pulse', orient='v', ax=axes[1],data=df2, showfliers=False)

for ax in axes.flatten():
    for lbl, patch in zip(ax.get_xticklabels() , ax.patches):
        if lbl.get_text() == 'rest':
            patch.set_hatch('/')
            patch.set_edgecolor(patch.get_facecolor())
            patch.set_facecolor('none')

plt.show()

输出:

相关问题