Skip to content Skip to sidebar Skip to footer

Matplotlib: How To Have A Transparent Box Plot Face While A Non-transparent Line Edge?

I'm building a function to draw custom box plots. I'd like the face of the boxes to be plain with slight transparency and I'd like a thin solid (non-transparent) line around each

Solution 1:

set_alpha changes both face color and edge color, to avoid that, you may want to consider passing RGBA values directly to face color:

#cols = ['red', 'blue', 'green']
cols = [[1,0,0,0.5],
        [0,1,0,0.5],
        [0,0,1,0.5]]

controls = ['trt_a', 'trt_b', 'trt_c']

fig, ax = plt.subplots()

boxplot_dict = ax.boxplot([data[x] for x in ['a', 'b', 'c']], \
    positions = [1, 1.5, 2], labels = controls, \
    patch_artist = True, widths = 0.25)

for b, c in zip(boxplot_dict['boxes'], cols):
    #b.set_alpha(0.6)
    b.set_edgecolor('k') # or try 'black'
    b.set_facecolor(c)
    b.set_linewidth(1)

enter image description here

Solution 2:

You can try and set facecolor = '#1b9e77' for boxprops, this can be a transparent color or solid one.

plt.boxplot(... , boxprops=dict(facecolor=(0,0,0,0))

Post a Comment for "Matplotlib: How To Have A Transparent Box Plot Face While A Non-transparent Line Edge?"