Sherin Jayanand
Sherin Jayanand

Reputation: 204

Mapping color to a particular value df.plot

I have a dataframe with animal names in it, like:-

cat
dog
pig
lion
tiger
goat
dog
dog
goat
pig
cat
lion

I want to draw a horizontal bar graph using:-

c=['green','pink','blue','yellow','cyan','teal','red','violet']    
df.animal.value_counts().sort_values().plot(kind='barh', color=c, alpha=0.5)

This works well.

Image showing the initial graph

But every time count of the animal changes the colour for that bar changes.

Image showing how the colour for an animal changes when the count increases

I want to have a consistent colour for an animal, say "blue" for "cat" and "green" for "dog" and so on. How do I do this?

This dataset is continuously evolving and can have newer animals names with time and I wish to ensure that a new colour is automatically assigned to an animal as it gets added. Even if this cannot be done I would be grateful if you can help with the initial request.

I tried various options as found on StackOverflow and otherwise but am not getting what I want.

Upvotes: 0

Views: 596

Answers (1)

JohanC
JohanC

Reputation: 80279

The following approach uses a dictionary to assign a fixed color to each animal. For the sorted dataframe, the dictionary is applied to each element of the index.

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

animals = ['cat', 'dog', 'goat', 'lion', 'pig', 'tiger']
color_dict = {'cat': 'turquoise', 'dog': 'sienna', 'goat': 'springgreen',
              'lion': 'gold', 'pig': 'deeppink', 'tiger': 'darkorange'}

fig, axs = plt.subplots(ncols=3, figsize=(12, 3))
for i, ax in enumerate(axs):
    df = pd.DataFrame({'animal': np.random.choice(animals, 40)})
    df_counts = df.animal.value_counts().sort_values()
    # c = [color_dict[a] for a in df_counts.index]
    c = df_counts.index.map(color_dict)
    df_counts.plot(kind='barh', color=c, alpha=0.8, ax=ax, title=f'test {i+1}')
    for j, cnt in enumerate(df_counts):
        ax.text(cnt, j, f'{cnt} ', ha='right', va='center', c='black')
plt.show()

example plot

Upvotes: 1

Related Questions