'Add a custom value on seaborn boxplots graphs

I am having trouble with a specific demand on my graphs. For now, I had to do the following instructions:

  • Read two dataframes
  • Create boxplots for the first dataframe and color the boxplots depending on the values of the second dataframe (the code is below, and more information are in my previous StackQuestion)

The code below works and my problems come after:

df=pd.DataFrame([['A',10, 22], ['A',12, 15], ['A',0, 2], ['A', 20, 25], ['A', 5, 5], ['A',12, 11], ['B', 0 ,0], ['B', 9 ,0], ['B', 8 ,50], ['B', 0 ,0], ['B', 18 ,5], ['B', 7 ,6],['C', 10 ,11], ['C', 9 ,10], ['C', 8 ,2], ['C', 6 ,2], ['C', 8 ,5], ['C', 6 ,8]],
         columns=['Name', 'Value_01','Value_02'])
df_agreement=pd.DataFrame([['A', '<66%', '>80'],['B', '>80%', '>66% & <80%'], ['C', '<66%', '<66%']], columns=['Name', 'Agreement_01', 'Agreement_02']) 
fig = plt.figure()
# Change seaborn plot size
fig.set_size_inches(60, 40)
plt.xticks(rotation=70)
plt.yticks(fontsize=40)
df_02=pd.melt(df, id_vars=['Name'],value_vars=['Value_01', 'Value_02'])
bp=sns.boxplot(x='Name',y='value',hue="variable",showfliers=True, data=df_02,showmeans=True,meanprops={"marker": "+", 
                       "markeredgecolor": "black", 
                       "markersize": "20"})
bp.set_xlabel("Name", fontsize=45)
bp.set_ylabel('Value', fontsize=45)
handles, labels = bp.get_legend_handles_labels()
new_handles = handles + [plt.Rectangle((0, 0), 0, 0, facecolor="#D1DBE6", edgecolor='black', linewidth=2),
                         plt.Rectangle((0, 0), 0, 0, facecolor="#EFDBD1", edgecolor='black', linewidth=2)]
bp.legend(handles=new_handles,
          labels=['V_01', 'V_02', "V_01 with less\n than 66% agreement", "V_02 with less\n than 66% agreement"])
list_color_1=[]
list_color_2=[]
for i in range(0, len(df_agreement)):
    name=df_agreement.loc[i,'Name']
    if df_agreement.loc[i,'Agreement_01']=="<66%":
        list_color_1.append(i*2)
    if df_agreement.loc[i,'Agreement_02']=="<66%":
        list_color_2.append(i*2+1)
for k in list_color_1:
    mybox = bp.artists[k]
    # Change the appearance of that box
    mybox.set_facecolor("#D1DBE6") #facecolor is the inside color of the boxplot
    mybox.set_edgecolor('black') #edgecolor is the line color of the box
    mybox.set_linewidth(2)
for k in list_color_2:
    mybox = bp.artists[k]
    # Change the appearance of that box
    mybox.set_facecolor("#EFDBD1") #facecolor is the inside color of the boxplot
    mybox.set_edgecolor('black') #edgecolor is the line color of the box
    mybox.set_linewidth(2)

Now I have a new dataFrame, equivalent to the first one (df), but with different values:

df_02=pd.DataFrame([['A',5, 20], ['A',15, 2], ['A',3, 5], ['A', 21, 24], ['A', 6, 6], ['A',10, 10], ['B', 0 ,0], ['B', 9 ,0], ['B', 9 ,5], ['B', -4 ,-2], ['B', 8 ,7], ['B', 8 ,9],['C', 10 ,15], ['C', 9 ,10], ['C', 8 ,2], ['C', 6 ,2], ['C', 8 ,5], ['C', 6 ,8]],
         columns=['Name', 'Value_01','Value_02'])

What I would like to do is that on the boxplots, I would add a bar (only on each boxplot) corresponding to the value of my second dataframe (df_02).

Is there anyone who would have a guess for that one ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source