Reds in hue / palette based line art

I am trying to set the edge color for a barplot

created with seaborn

. The problem is I am using the hue parameter.

Rather than having a separate color for each individual panel, the edgecolor parameter applies the color to the entire hue / group.

Reproduce the problem with this simple example.

tips = sns.load_dataset("tips")
t_df = tips.groupby(['day','sex'])['tip'].mean().reset_index()

      

Hence t_df will be,

enter image description here

clrs = ["#348ABD", "#A60628"]
t_ax = sns.barplot(x='day',y='tip',hue='sex',data=t_df,alpha=0.75,palette= sns.color_palette(clrs),edgecolor=clrs)
plt.setp(t_ax.patches, linewidth=3)   # This is just to visualize the issue.

      

The result that gives

enter image description here

I want the blue stripe to be blue and the same red. What code change would it require?

+3


source to share


1 answer


This is somewhat hacky, but it gets the job done:

enter image description here

import matplotlib.patches

# grab everything that is on the axis
children = t_ax.get_children()

# filter for rectangles
for child in children:
    if isinstance(child, matplotlib.patches.Rectangle):

        # match edgecolors to facecolors
        clr = child.get_facecolor()
        child.set_edgecolor(clr)

      



EDIT:

@mwaskom's suggestion is clearly much cleaner. For completeness:

for patch in t_ax.patches:
    clr = patch.get_facecolor()
    patch.set_edgecolor(clr)

      

+2


source







All Articles