Skip to content
Advertisement

How to assign different palettes to hue levels in a Seaborn barplot?

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:

JavaScript

enter image description here

This code almost works:

JavaScript

but has 2 problems:

  1. No Legend
  2. The bars are not separated.

enter image description here

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:

  1. seaborn barplot: vary color with x and hue
  2. Set color-palette in Seaborn Grouped Barplot depending on values

Advertisement

Answer

You could create the bar plot using grey and black as hue-colors for the legend. And, afterwards, loop through the created bars and change their color.

JavaScript

sns.barplot with adapted colors

To get all the variations into the legend, the TupleHandler can be used, for example (using colors=['crimson', 'limegreen', 'dodgerblue']):

JavaScript

using a TupleHandler for the legend

Here is another example:

JavaScript

tips dataset sns.barplot colors per day

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