问题在多个箱线图图表的颜色方面

huangapple go评论91阅读模式
英文:

Problem in colors of multiple boxplot charts

问题

我想从Excel文件中创建多个箱线图。我的问题是所有箱子都显示相同的颜色(深蓝色),然而我没有定义这样的颜色!这是我的代码,并且它指定了我想要的颜色:

英文:

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 !

答案1

得分: 0

你在循环中调用 axs[i].boxplot(...),同时设置了每个设置,如箱线图的颜色和样式。这会每次创建一个新的箱线图,这就是不起作用的原因。因为我没有你的数据,我正在使用 seaborn 提供的泰坦尼克数据集,并展示了处理三个变量的正确方法。请注意,您需要首先将箱线图分配给 box(仅在循环中创建一次),然后调用其中的每个属性。您可以将其设置为您的数据,应该可以正常工作。希望这是您要寻找的...

英文:

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

问题在多个箱线图图表的颜色方面

huangapple
  • 本文由 发表于 2023年4月10日 23:14:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定