I have a seaborn boxplot with a categorical variable to use for hue and a dictionary with a color for each category given as the palette argument. MWE:
import seaborn as sns
from matplotlib import pyplot as plt
cdict = {"First" : "gold",
"Second": "blue",
"Third" : "red"}
df = sns.load_dataset("titanic")[["sex","fare","class"]]
fig, ax = plt.subplots()
sns.boxplot(data=df, x="sex", y="fare", hue="class",palette=cdict, ax=ax)
plt.show()
Giving: I would like to have the fliers in the same color as the boxes (face or edge color). My plot is relatively crowded, so without this it is difficult to quickly see which outlier corresponds to which category.
As you are using the hue parameter I don't think you can pass through the flierprops
keyword argument to sns.boxplot
. Instead we can iterate over the containers/boxes and dynamically fetch/set the colours based on the boxes that we see
fig, ax = plt.subplots()
sns.boxplot(data=df, x="sex", y="fare", hue="class", palette=cdict, ax=ax)
for container in ax.containers:
for box in container:
current_colour = box.box.get_facecolor()
box.fliers.set_markerfacecolor(current_colour)
# uncomment to set edge colour
# box.fliers.set_markeredgecolor(current_colour)