I'm using seaborn
and pandas
to create some bar plots from different (but related) data. The two datasets share a common category used as a hue
, and as such I would like to ensure that in the two graphs the bar colour for this category matches. How can I go about this?
A basic example is as follows:
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
sns.set_style('darkgrid')
fig, ax = plt.subplots()
a = 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')
In this example, I would like to ensure that the Total
category uses the same colour in both graphs (e.g. black)