Seaborn 箱线图,所有箱子颜色相同

Seaborn Boxplot with Same Color for All Boxes

我正在使用 seaborn 并想生成一个所有框都具有相同颜色的箱线图。出于某种原因,seaborn 为每个框使用不同的颜色,并且没有选项可以停止此行为并为所有框设置相同的颜色。

如何强制 seaborn 对所有框使用相同的颜色?

fig, ax = plt.subplots(figsize=(10, 20))
sns.boxplot(y='categorical_var', x='numeric_var', ax=ax)

制作您自己的调色板并将方框的颜色设置为:

import seaborn as sns
import matplotlib.pylab as plt

sns.set_color_codes()
tips = sns.load_dataset("tips")
pal = {day: "b" for day in tips.day.unique()}
sns.boxplot(x="day", y="total_bill", data=tips, palette=pal)

plt.show()

另一种方法是遍历箱线图的艺术家,并为轴距的每个艺术家设置颜色 set_facecolor

ax = sns.boxplot(x="day", y="total_bill", data=tips)
for box in ax.artists:
    box.set_facecolor("green")

使用color参数:

import seaborn as sns
tips = sns.load_dataset("tips")
sns.boxplot(x="day", y="tip", data=tips, color="seagreen")