Skip to content
Advertisement

Color Bar Chart Based on Label Name

I am new to python so please feel free to explain like I am a 2yr old.

I am trying to process a spreadsheet and create multiple bar charts by filtering the content based on strings in the row. I now want to ensure that all the bar charts have a consistent bar color scheme based on the label. e.g. Effective=green, Not Effective=red, Not Applicable=gray etc. Not all labels will be in every bar chart so I cant rely on ordering the colors, I need to be specific by assigning a pairing. I assume I need to create an index and then a color mapping but I am unsure how to do this.

Code is;

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

filtered_audit_result_columns = pd.read_csv('audit_results.csv', usecols=["Title", "Implementation Status"])

guidelines_for_data_centre = filtered_audit_result_columns [filtered_audit_result_columns ['Title'].str.contains('Guidelines for Data Centres')]

print(guidelines_for_data_centre)
                              Title Implementation Status
0   Guidelines for Data Centres             Effective
1   Guidelines for Data Centres         Not Effective
2   Guidelines for Data Centres         Not Effective
3   Guidelines for Data Centres         Not Effective
4   Guidelines for Data Centres             Effective
5   Guidelines for Data Centres        Not Applicable
6   Guidelines for Data Centres             Effective
7   Guidelines for Data Centres         Not Effective
8   Guidelines for Data Centres         No Visibility
9   Guidelines for Data Centres         No Visibility
10  Guidelines for Data Centres        Not Applicable
11  Guidelines for Data Centres         No Visibility
12  Guidelines for Data Centres             Effective
13  Guidelines for Data Centres        Not Applicable
count = guidelines_for_data_centre['Implementation Status'].value_counts()

print(count)
################################
Not Effective     4
Not Applicable    3
Effective         4
No Visibility     3
################################
hbar = count.plot.barh(x= 'Implementation Status')

Any advice would be appreciated.

Thank you

Advertisement

Answer

If you work with seaborn, you can use a dictionary as palette. You can also call sns.countplot which already does the counting (and skip count_values()).

Here is a code example:

import seaborn as sns
import pandas as pd
import numpy as np

my_color_dict = {'Effective': 'limegreen', 'Not Effective': 'crimson',
                 'Not Applicable': 'silver', 'Not Visible': 'skyblue'}
guidelines_for_data_centre = pd.DataFrame({'Implementation Status': np.random.choice(
     ['Effective', 'Not Effective', 'Not Applicable', 'Not Visible'], 100, p=[0.2, 0.5, 0.3, 0.0])})
sns.countplot(data=guidelines_for_data_centre, x='Implementation Status', palette=my_color_dict)

sns.countplot with consistent colors

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement