Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Seaborn Concatenated Bar Charts Different Colors

I have this dataframe called cases_deaths:

   week daily_case_totals   daily_death_totals
0   1   2.0                 0.0
1   2   12.0                0.0
2   3   12.0                0.0
3   4   2.0                 0.0
4   5   573.0               6.0
5   6   3134.0              12.0
6   7   3398.0              32.0
7   8   992.0               25.0
.
.
.

And this code to generate to Seaborn charts:

fig, axes = plt.subplots(2, 1, figsize=(11, 10))
for name, ax in zip(['daily_case_totals', 'daily_death_totals'], axes):
    sns.barplot(data=cases_deaths, x='week', y=name, ax=ax, color = 'red')

And the chart looks like this:
chart

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But I want the top one to be blue and bottom to be red. Not sure how to do that, I’ve tried passing in a list of colors to the color parameter in the for loop but that yielded an error.

>Solution :

Just add one more iterable to zip for the colors:

import seaborn as sns
fig, axes = plt.subplots(2, 1, figsize=(11, 10))
for name, color, ax in zip(('daily_case_totals', 'daily_death_totals'),
                           ('blue', 'red'),
                           axes):
    sns.barplot(data=cases_deaths, x='week', y=name, ax=ax, color=color)

multicolor plot

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading