多个箱线图的颜色问题

Problem in colors of multiple boxplot charts

提问人:Omid MansouriHanis1 提问时间:4/10/2023 更新时间:4/11/2023 访问量:100

问:

我想从 excel 文件创建多个箱线图。我的问题是所有 boxex 都获得相同的颜色(深蓝色),但我没有定义这样的颜色! 这是我的代码,它指定了我想要什么颜色:

import pandas as pd
import matplotlib.pyplot as plt

# load the Excel file into a pandas dataframe
df = pd.read_excel('D:\Omid_TTU\RA\TASK5\selected1daybefore&after\GIS_Standard_format\FID_6_test2.xlsx')

# create a grid of subplots
fig, axs = plt.subplots(nrows=1, ncols=5, figsize=(20, 5))

# loop through each column and plot it in its own subplot
for i, col in enumerate(['Liq_depth_dim', 'TMP_air_temp', 'VIS_dist_dim', 'WND_dir_ang', 'WND_speed_rate']):
    data = df[col].dropna()
    axs[i].boxplot(data, patch_artist=True, notch=True, vert=False)
    axs[i].set_title(col)
    axs[i].set_yticklabels([])

    color_list = ['red', 'lightyellow', 'green', 'slateblue2', 'steelblue1']
    for patch, colormap in zip(axs[i].boxplot(data, patch_artist=True, notch=True, vert=False)['boxes'], color_list):
        patch.set_facecolor(colormap)

    for whisker in axs[i].boxplot(data, patch_artist=True, notch=True, vert=False)['whiskers']:
        whisker.set(color='r', linewidth=3, linestyle=':')

    for cap in axs[i].boxplot(data, patch_artist=True, notch=True, vert=False)['caps']:
        cap.set(color='r', linewidth=2)

    for median in axs[i].boxplot(data, patch_artist=True, notch=True, vert=False)['medians']:
        median.set(color='g', linewidth=3)

    for flier in axs[i].boxplot(data, patch_artist=True, notch=True, vert=False)['fliers']:
        flier.set(marker='D', color='r', alpha=0.5)

# adjust the spacing between subplots
plt.subplots_adjust(wspace=0.5)

plt.show()

你能帮我解决这个问题吗?

在此处输入图像描述

我想从 excel 文件创建多个箱线图。我的问题是所有 boxex 都获得相同的颜色(深蓝色),但我没有定义这样的颜色!

颜色 箱线图 枚举

评论


答:

0赞 Redox 4/11/2023 #1

您在 for 循环中调用 axs[i].boxplot(...),同时设置每个设置,如晶须、中位数等以及补丁。这是每次创建一个新的箱线图。这就是它不起作用的原因。由于我没有您的数据,我正在使用 seaborn 提供的泰坦尼克号数据集并展示 3 个变量的正确方法。请注意,您需要首先将箱线图(仅在 FOR 循环中创建一次)分配给箱线图,然后调用其中的每个属性。您可以将其设置为您的数据,它应该可以正常工作。希望这就是你要找的......box

import pandas as pd
import matplotlib.pyplot as plt

# load the Excel file into a pandas dataframe
#df = pd.read_excel('D:\Omid_TTU\RA\TASK5\selected1daybefore&after\GIS_Standard_format\FID_6_test2.xlsx')
df = sns.load_dataset("titanic")  ## My titanic data

# create a grid of subplots
fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(20, 5)) ## Changed ncols to 3, make it 5 for your data

# loop through each column and plot it in its own subplot
#for i, col in enumerate(['Liq_depth_dim', 'TMP_air_temp', 'VIS_dist_dim', 'WND_dir_ang', 'WND_speed_rate']):
for i, col in enumerate(['age', 'fare', 'pclass']): ##Changed cols for titanic
    data = df[col].dropna()
    box=axs[i].boxplot(data, patch_artist=True, notch=True, vert=False) ##Assigned boxplot to box - do only once inside one FOR loop
    axs[i].set_title(col)
    axs[i].set_yticklabels([])

    color_list = ['red', 'lightyellow', 'green', 'slateblue2', 'steelblue1']

    for patch in box['boxes']:
        patch.set_facecolor(color_list[i]) ## Face color
    for whisker in box['whiskers']:
        whisker.set(color='r', linewidth=3, linestyle=':') 
    for cap in box['caps']:
        cap.set(color='r', linewidth=2)
    for median in box['medians']:
        median.set(color='g', linewidth=3)
    for flier in box['fliers']:
        flier.set(marker='D', color='r', alpha=0.5)

plt.subplots_adjust(wspace=0.5)

plt.show()

输出图

enter image description here