4

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().

abukaj
  • 2,582
  • 1
  • 22
  • 45

2 Answers2

3

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.

Mathieu
  • 5,410
  • 6
  • 28
  • 55
  • 2
    Isn't that supposed to set both `edgecolor` and `facecolor`? – abukaj May 18 '18 at 13:47
  • @abukaj I just did a quick search in the doc of pyplot. From the example given, it should not. Again, I do not have data to test it, nor the time right now. Just check if it works :) – Mathieu May 18 '18 at 13:53
  • @abukaj Other version found which tends to show that `color` is specific to the line. – Mathieu May 18 '18 at 13:57
1

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)
Peter Csala
  • 17,736
  • 16
  • 35
  • 75