colorsboxplotenumerate

Problem in colors of multiple boxplot charts


I want to create multiple boxplot chart from an excel file. my problem is taht all boxex gain same color (dark blue) however I did not define such color ! this is my code and it specified that what colors are I want:

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()

could you please help me with this?

enter image description here

I want to create multiple boxplot chart from an excel file. my problem is taht all boxex gain same color (dark blue) however I did not define such color !


Solution

  • You are calling axs[i].boxplot(...) inside the for loop while setting each of your settings like whisker, median, etc. as well as patch. This is creating a new boxplot each time. That is the reason it is not working. As I dont have your data, I am using seaborn provided titanic dataset and showcasing the right way for 3 variables. Note that you need to first assign the boxplot (created only once inside the FOR loop to box and then calling each of the properties within it. You can set this to your data and it should work fine. Hope this is what you are looking for...

    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()
    

    Output plot

    enter image description here