I'm using Seaborn to create bar plots in Python. I have a 2 (attention: divided vs focused) X 3 (solutions: 1,2,3) design. For divided, I want the bar colors to be darkred, darkgreen, darkblue. For focused, I want the bar colors to be red, green, blue.
This code doesn't work because the palette only applies to the hue
parameter:
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('attention')
palette = ["darkred", "red", "darkgreen", "green", "darkblue", "blue"]
palette = ["darkred", "darkgreen", "darkblue", "red", "green", "blue"]
sns.set(style='ticks', context='notebook', font_scale=1.2)
fig, ax = plt.subplots(figsize=(7, 5), dpi=96)
my_plot = sns.barplot(x="solutions", y="score", hue="attention",
palette=palette, capsize=.1, ax=ax, data=data)
plt.show()
This code almost works:
import seaborn as sns
import matplotlib.pyplot as plt
data = sns.load_dataset('attention')
sns.set(style='ticks', context='notebook', font_scale=1.5)
fig, ax = plt.subplots(figsize=(7, 5), dpi=96)
sns.barplot(x="solutions", y="score",
palette=['red', 'green', 'blue'], capsize=.1, ax=ax,
data=data.query('attention=="focused"'))
sns.barplot(x="solutions", y="score",
palette=['darkred', 'darkgreen', 'darkblue'], capsize=.1, ax=ax,
data=data.query('attention=="divided"'))
sns.despine(top=True, right=True)
plt.tight_layout()
plt.show()
but has 2 problems:
- No Legend
- The bars are not separated.
There must (should?) be some way to create this figure with seaborn in an elegant way. The first approach is ideal: a single call to barplot with parameters specifying the graph. The second approach is ok: multiple calls to barplot. The lease desirable approach would be direct matplotlib bar() calls, but at this point I'd settle for any approach that led to the desired figure.
Note that, as far as I can tell, these similarly named questions do not appear to address the outcome being sought in this question: