Seaborn Drawer Box Assigns Custom Edges Colors from a Python List

I am trying to change the appearance of boxes in a Seaborn boxplot. I would like all the boxes to be transparent and the borders of the boxes to be listed. Here is the code I'm working with:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
fig, ax = plt.subplots()

df = pd.DataFrame(np.random.rand(10,4),columns=list('ABCD'))
df['E'] = [1,2,3,1,1,4,3,2,3,1]

sns.boxplot(x=df['E'],y=df['C'])

# Plotting the legend outside the plot (above)
box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9])
handles, labels = ax.get_legend_handles_labels()
leg = plt.legend(handles[0:2], labels[0:2],
                               loc='upper center', bbox_to_anchor=(0.5, 1.10), ncol=2)
plt.show()

This post shows how to change the color and rectangle by one square. However, I would like to assign box edgecolors based on a list like this box_line_col = ['r','g',b','purple']. The above code creates 4 fields on the chart - I would like to assign field colors for each field, starting from the first (extreme) field and continuing to the last (rightmost) field.

Is it possible to specify the colors of the edge of the box from the list while maintaining the transparent margins (facecolor = white)?

+2
1

. , plt.show() :

box_line_col = ['r','g','b','purple']

for i,box_col in enumerate(box_line_col):
    mybox = g.artists[i]
    mybox.set_edgecolor(box_col)
    mybox.set_fccecolor(None) #or white, if that what you want

    # If you want the whiskers etc to match, each box has 6 associated Line2D objects (to make the whiskers, fliers, etc.)
    # Loop over them here, and use the same colour as above
    for j in range(i*6,i*6+6):
        line = g.lines[j]
        line.set_color(box_col)
        line.set_mfc(box_col)
        line.set_mec(box_col)

plt.show()

, , - .

+2

All Articles