boxplot中的特定框分配颜色

boxplot中的特定框分配颜色

本文介绍了为seaborn.boxplot中的特定框分配颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大致按照以下方式调用seaborn.boxplot:

I'm calling seaborn.boxplot roughly as follows:

   seaborn.boxplot(ax=ax1,
                    x="centrality", y="score", hue="model", data=data],
                    palette=seaborn.color_palette("husl", len(models) +1),
                    showfliers=False,
                    hue_order=order,
                    linewidth=1.5)

是否可以通过给其提供特定的颜色来使一个盒子脱颖而出,同时使用给定的调色板为所有其他盒子着色?

Is it possible to make one box stand out by giving it a specific color, while coloring all others with the given color palette?

推荐答案

使用sns.boxplot制作的盒子实际上只是matplotlib.patches.PathPatch对象.这些作为列表存储在ax.artists中.

The boxes made using sns.boxplot are really just matplotlib.patches.PathPatch objects. These are stored in ax.artists as a list.

因此,我们尤其可以通过为ax.artists编制索引来选择一个框.然后,您可以设置facecoloredgecolorlinewidth以及许多其他属性.

So, we can select one box in particular by indexing ax.artists. Then, you can set the facecolor, edgecolor and linewidth, among many other properties.

例如(基于以下示例之一此处):

For example (based on one of the examples here):

import seaborn as sns
import matplotlib.pyplot as plt

sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
ax = sns.boxplot(x="day", y="total_bill", hue="smoker",
                 data=tips, palette="Set3")

# Select which box you want to change
mybox = ax.artists[2]

# Change the appearance of that box
mybox.set_facecolor('red')
mybox.set_edgecolor('black')
mybox.set_linewidth(3)

plt.show()

这篇关于为seaborn.boxplot中的特定框分配颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:16