2
votes

When using hue parameter on barplot in Seaborn it changes the color and the position of the bars. Like the following exemple:

Plot without hue:

import seaborn as sns
df = {'x': [1, 2, 3, 4], 'y': [5, 6, 7, 8], 'hue': ['a', 'b', 'b', 'a']}
sns.barplot(
    data = df,
    x = 'x',
    y = 'y',
)

Output: Seaborn barplot without hue

Plot with hue:

df = {'x': [1, 2, 3, 4], 'y': [5, 6, 7, 8], 'hue': ['a', 'b', 'b', 'a']}
sns.barplot(
    data = df,
    x = 'x',
    y = 'y',
    hue = 'hue'
)

Output: Seaborn barplot with hue

So how can I make a plot with the default bar position and the colors made by hue?

1

1 Answers

2
votes

Setting dodge=False fixes it for me.

import seaborn as sns

df = {'x': [1, 2, 3, 4], 'y': [5, 6, 7, 8], 'hue': ['a', 'b', 'b', 'a']}
sns.barplot(
    data = df,
    x = 'x',
    y = 'y',
    hue = 'hue',
    dodge=False 
)

From the docs:

dodge (bool, optional) - When hue nesting is used, whether elements should be shifted along the categorical axis.