0

I would like to customize different linecolors for diffferent box in seaborn.boxplot, here is a toy example:

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

figure(figsize=(8, 6), dpi=80)

PROPS = {
    'boxprops':{'facecolor':'none', 'edgecolor':'red'},
    'medianprops':{'color':'green'},
    'whiskerprops':{'color':'blue'},
    'capprops':{'color':'yellow'}
}

df_dict={'court':[1,1,2,2,3,3,4,4],
         'player':['Bob','Ian','Bob','Ian','Bob','Ian','Ian','Bob'],
         'score':[6,8,12,15,8,16,11,13],
         'win':['no','yes','no','yes','no','yes','no','yes']}

df=pd.DataFrame.from_dict(df_dict)

ax = sns.boxplot(y='score',x='player',data=df,**PROPS)
ax = sns.swarmplot(y='score',x='player',hue='win',data=df,s=5,palette=['red','green'])

sns.lineplot(
    data=df, y="score", x="player", units="court",
    color="#bdbdbd", estimator=None
)
plt.show()

enter image description here Here, the parameter 'PROPS' works for each box. How can I set different 'boxprops', 'medianprops', 'whiskerprops' and 'capprops' for the two boxes seperately?

I have found a similar question in assign a color to a specific box in seaborn.boxplot. However, this question is too old and the answers not worked for me.

Knotnet
  • 21
  • 3
  • The [accepted answer](https://stackoverflow.com/a/36331317/7758804) to [assign a color to a specific box in seaborn.boxplot](https://stackoverflow.com/q/36305695/7758804), works without issue. To iterate through each boxplot: `for p in [pp for pp in ax.patches if type(pp) == mpl.patches.PathPatch]:` – Trenton McKinney Jun 18 '23 at 19:08

0 Answers0