Jishan
Jishan

Reputation: 1684

Grouped Boxplot Alternate Color, Label and Tight Spacing Issue

With the following data (for a quick csv file generation, dataset link):

dataset

and the following code:

df_all[['all', 'taste','fruit']].melt('fruit').boxplot(column=['value'], by=['fruit', 'variable'], rot=45, fontsize=11, patch_artist=True,
             color=dict(boxes='#053061', whiskers='#67001F', medians='#A7D0E4', caps='#67001F'),
             boxprops=dict(linestyle='-', linewidth=0.8),
             flierprops=dict(linestyle='-', linewidth=0, marker='+'),
             medianprops=dict(linestyle='-', linewidth=0.8),
             whiskerprops=dict(linestyle='-', linewidth=0.8),
             capprops=dict(linestyle='-', linewidth=0.8),
             showfliers=True, 
             grid=False,)

I get this abomination:

abomination

I want to achieve

  1. alternate colors on boxplots (first blue as is, then red and every alternate one red)
  2. label below every box plot
  3. legend

something like:

enter image description here

Probably it is impossible to achieve an exact output, but any help is really appreciated!

Upvotes: 2

Views: 158

Answers (1)

tdy
tdy

Reputation: 41327

If you're open to using seaborn, you can use sns.boxplot() and set the melted variable as hue:

import seaborn as sns
sns.boxplot(
    data=df[['all', 'taste', 'fruit']].melt('fruit'),
    x='fruit',
    y='value',
    hue='variable',
    width=0.5,
    order=['apple', 'mango', 'orange'],
)

seaborn grouped boxplot

Upvotes: 3

Related Questions