abukaj
abukaj

Reputation: 2712

Border colour of matplotlib filled boxplots

I am trying to plot a boxplot which box is filled. I would also like it to have a custom-colour border (different than the whiskers are).

I have found an answer how to enable filled boxplots. Unfortunately the 'edgecolor' boxprops property does not work as expected:

plt.boxplot(np.random.normal(size=1000),
            patch_artist=True,
            boxprops={'facecolor': '#AAAAAA',
                      'edgecolor': '#FFCC00'})

results with:

enter image description here

How can I change the border colour of the box alone? Preferably in call to the plt.boxplot().

Upvotes: 4

Views: 7950

Answers (2)

In addition to Mathieu's answer, sometimes color may work (not sure what changes it) and sometimes edgecolor will work to change the edges of the boxes.

for box in bp['boxes']:
    box.set(edgecolor='black', linewidth=1)

Upvotes: 1

Mathieu
Mathieu

Reputation: 5746

I think the right argument for the boxprops is not edgecolor but color:

boxprops = dict(linestyle='-', linewidth=1, color='#FFCC00')

Other version found, to test:

# Create the boxplot
bp = ax.boxplot(data_to_plot)

for box in bp['boxes']:
    # change outline color
    box.set(color='#7570b3', linewidth=2)
    # change fill color
    box.set(facecolor = '#1b9e77' )

The key word color seems specific to the lines.

Upvotes: 3

Related Questions