2

enter image description hereI mad a boxplot from a df (pets) with the type of animal and age of each animal. I need the cat box to be purple, dog to be green, and parrot to be orange. I can't figure out how to change the the color of the individual boxes.

I have been on stack all day trying different codes. Not sure in particular what I have tried though. I am relatively new to python.

    import matplotlib.pyplot as pl t
    import numpy as np
    import pandas as pd
    pets = pd.read_csv('pets (1).csv')
    boxplot = pets.boxplot(column=['Age'], by=['Kind'], patch_artist = True)

I have a boxplot that shows the 3 different animal kinds by age, but they are all filled in blue and need the colors to be different for each one.

  • See [this answer](https://stackoverflow.com/questions/39297093/change-the-facecolor-of-boxplot-in-pandas). Using `seaborn` [boxplot](https://seaborn.pydata.org/generated/seaborn.boxplot.html) is super easy with `pandas` dataframes. – m13op22 May 10 '19 at 20:46
  • Possible duplicate of [Change color of individual boxes in pandas boxplot subplots](https://stackoverflow.com/questions/50963960/change-color-of-individual-boxes-in-pandas-boxplot-subplots) – m13op22 May 10 '19 at 21:00

1 Answers1

0

You could do this easily in seaborn.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

sns.set_palette(['purple', 'green', 'orange'])
pets = pd.DataFrame({'Age': np.random.rand(10), 'Kind': ['Cat']*3+['Dog']*3 + ['Parrot']*4})
sns.boxplot(x='Kind', y='Age', data = pets)

enter image description here

m13op22
  • 2,168
  • 2
  • 16
  • 35