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;

JavaScript
JavaScript
JavaScript
JavaScript
JavaScript

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:

JavaScript

sns.countplot with consistent colors

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