如何为每个箱线图设置不同的颜色

问题描述 投票:0回答:1

我正在尝试在 VSCode 中制作 Seaborn 箱线图。我的代码基于这里的示例:here。我专门制作了类似倒数第二个示例的东西,但没有注释。

代码

# 0. Import the modules
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns


# 1. Import the data
random_df = pd.DataFrame(data = {'0': np.random.rand(10),
                                 '1': np.random.rand(10),
                                 '2': np.random.rand(10),
                                 '3': np.random.rand(10),
                                 '4': np.random.rand(10)})

# 2. Do the plotting

# set style - When adding multple boxplots I like use whitegird 
sns.set(style='whitegrid')

fig, ax = plt.subplots(figsize=(12,9))
g = sns.boxplot(data = random_df, width = 0.7)              

# with a descriptive title a ylabel might not be necessary
plt.ylabel("Accuracy", fontsize = 14)

# X tick-labels
# we are including this because I want the full product name not the variable name
xvalues = ["Label 1", "Label 2", "Label 3", "Label 4", "Label 5"] 

# set xvalues as xtick values
plt.xticks(np.arange(5), xvalues)

# remove all borders except bottom
sns.despine(top=False,
            right=True,
            left=True,
            bottom=False)

# Set colors of box plots 
palette= ['plum','g','orange','b','r']
color_dict = dict(zip(xvalues, palette))

for i in range(0,5):
    mybox = g.artists[i]
    mybox.set_facecolor(color_dict[xvalues[i]])  

plt.tight_layout()
plt.show()

问题:

当我在 VSCode 中运行代码时,出现以下错误:“索引超出范围”。这与

g.artists[i]
行有关;当我从代码中取出
for
循环时,箱线图就可以工作了。另外,当我在 VSCode 中使用此代码时,它只会产生错误。当我在 Google Colab 中运行代码时,没有错误。

python matplotlib seaborn boxplot
1个回答
1
投票

在 matplotlib 3.5 中,框存储在

ax.patches
而不是
ax.artists

在 Seaborn 中更改颜色的推荐方法是通过 pandas 的 melt()

 将数据帧转换为 
long form,然后在与
hue=
相同的变量上使用
x=
palette=
。 (
palette
可以是字典或颜色列表等。)

通过临时更改列名称,将自动设置 x 刻度标签。如果

ax
已预先创建,则将其作为参数传递会告诉seaborn 绘制到该轴上。 (Axes-level 函数返回创建绘图的
ax
;当
ax
作为参数给出时,无需存储返回值。)

这是一个例子:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

random_df = pd.DataFrame(data={'0': np.random.rand(10),
                               '1': np.random.rand(10),
                               '2': np.random.rand(10),
                               '3': np.random.rand(10),
                               '4': np.random.rand(10)})
sns.set(style='whitegrid')

fig, ax = plt.subplots(figsize=(12, 9))
xvalues = ["Label 1", "Label 2", "Label 3", "Label 4", "Label 5"]
palette = ['plum', 'g', 'orange', 'b', 'r']

melted_df = random_df.set_axis(xvalues, axis=1).melt(var_name='Variable', value_name='Accuracy')
sns.boxplot(data=melted_df, x='Variable', y='Accuracy', hue='Variable', palette=palette,
            width=0.7, dodge=False, ax=ax)

ax.legend_.remove()  # remove the legend, as the information is already present in the x labels
ax.set_xlabel('')  # remove unuseful xlabel ('Variable')
ax.set_ylabel("Accuracy", fontsize=14)

sns.despine(top=True, right=True, left=True, bottom=False)
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.