python - Seaborn - change bar colour according to hue name -


i'm using seaborn , pandas create bar plots different (but related) data. 2 datasets share common category used hue, , such ensure in 2 graphs bar colour category matches. how can go this?

a basic example follows:

import seaborn sns import pandas pd import matplotlib.pyplot plt sns.set_style('darkgrid') fig, ax = plt.subplots()  = pd.dataframe({'program': ['a', 'a', 'b', 'b', 'total', 'total'],                   'scenario': ['x', 'y', 'x', 'y', 'x', 'y'],                   'duration': [4, 3, 5, 4, 9, 7]})  g = sns.barplot(data=a, x='scenario', y='duration',                 hue='program', ci=none) plt.tight_layout() plt.savefig('3 progs.png')  plt.clf()  b = pd.dataframe({'program': ['a', 'a', 'b', 'b', 'c', 'c', 'total', 'total'],                   'scenario': ['x', 'y', 'x', 'y', 'x', 'y', 'x', 'y'],                   'duration': [4, 3, 5, 4, 3, 2, 12, 9]})  g = sns.barplot(data=b, x='scenario', y='duration',                 hue='program', ci=none) plt.tight_layout() plt.savefig('4 progs.png') 

producing 2 graphs: 3 category bar plot 4 category bar plot

in example, ensure total category uses same colour in both graphs (e.g. black)

a. using list of colors

the easiest solution make sure have same colors same categories in both plots manually specify colors @ plot creation.

# first bar plot ax = sns.barplot(data=a, x='scenario', y='duration',                 hue='program', ci=none, palette=["c0", "c1", "k"])  # ... # second bar plot ax2 = sns.barplot(data=b, x='scenario', y='duration',                 hue='program', ci=none,  palette=["c0", "c1","c2", "k"]) 

the color "c2" (the third color of color cycle) present in second plot there exists programm c.

b. using dictionary

instead of list, may use dictionary, mapping values hue column colors.

palette ={"a":"c0","b":"c1","c":"c2", "total":"k"}  ax = sns.barplot(data=a, x='scenario', y='duration', hue='program', palette=palette) # ... ax2 = sns.barplot(data=b, x='scenario', y='duration', hue='program', palette=palette) 

in both cases, output this:
enter image description here

c. automatic dictionary

finally, may create dictionary automatically values hue column. advantage here neither need know colors, nor values in respective dataframes beforehands.

import seaborn sns import pandas pd import matplotlib.pyplot plt sns.set_style('darkgrid') fig, ax = plt.subplots()  = pd.dataframe({'program': ['a', 'a', 'b', 'b', 'total', 'total'],                   'scenario': ['x', 'y', 'x', 'y', 'x', 'y'],                   'duration': [4, 3, 5, 4, 9, 7]}) b = pd.dataframe({'program': ['a', 'a', 'b', 'b', 'c', 'c', 'total', 'total'],                   'scenario': ['x', 'y', 'x', 'y', 'x', 'y', 'x', 'y'],                   'duration': [4, 3, 5, 4, 3, 2, 12, 9]})  unique = a["program"].append(b["program"]).unique() palette = dict(zip(unique, sns.color_palette())) palette.update({"total":"k"})  ax = sns.barplot(data=a, x='scenario', y='duration',                 hue='program', ci=none, palette=palette) plt.tight_layout() plt.figure()  ax2 = sns.barplot(data=b, x='scenario', y='duration',                 hue='program', ci=none,  palette=palette) plt.tight_layout() plt.show() 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -