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

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

此问题在此处已有答案

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

  1. import seaborn as sns
  2. exercise = sns.load_dataset("exercise")
  3. df1=exercise.loc[(exercise["diet"]=="low fat"),:]
  4. df2=exercise.loc[(exercise["diet"]=="no fat"),:]
  5. fig, axes = plt.subplots(1, 2)
  6. ax1=sns.boxplot(x='kind', y='pulse', orient='v', ax=axes[0],data=df1, showfliers=False)
  7. ax2= sns.boxplot(x='kind', y='pulse', orient='v', ax=axes[1],data=df2, showfliers=False)

rsl1atfo

rsl1atfo1#

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

  1. import seaborn as sns
  2. import matplotlib.pyplot as plt
  3. exercise = sns.load_dataset("exercise")
  4. df1=exercise.loc[(exercise["diet"]=="low fat"),:]
  5. df2=exercise.loc[(exercise["diet"]=="no fat"),:]
  6. fig, axes = plt.subplots(1, 2)
  7. sns.boxplot(x='kind', y='pulse', orient='v', ax=axes[0],data=df1, showfliers=False)
  8. sns.boxplot(x='kind', y='pulse', orient='v', ax=axes[1],data=df2, showfliers=False)
  9. for ax in axes.flatten():
  10. for lbl, patch in zip(ax.get_xticklabels() , ax.patches):
  11. if lbl.get_text() == 'rest':
  12. patch.set_hatch('/')
  13. patch.set_edgecolor(patch.get_facecolor())
  14. patch.set_facecolor('none')
  15. plt.show()

输出:

展开查看全部

相关问题