How to use hue, color, edgecolor, and facecolor in seaborn

Viewed 824

I have a dataset that looks like this:

{'prediction': {5: 'c1',
  4: 'c1',
  3: 'c1',
  2: 'c1',
  0: 'c1',
  1: 'c1',
  7: 'c1',
  6: 'c1'},
 'variable': {5: 'ft1',
  4: 'ft2',
  3: 'ft3',
  2: 'ft4',
  0: 'ft5',
  1: 'ft6',
  7: 'ft7',
  6: 'ft8'},
 'value': {5: 0.020915912763961077,
  4: 0.020388363414781133,
  3: 0.007204373035913109,
  2: 0.0035298765062560817,
  0: -0.002049702058734183,
  1: -0.004283512505036808,
  7: -0.01882610282871816,
  6: -0.022324439779323434}}

I am trying to make a barplot, which works, below:

sns.barplot(data=x, x='value', y='variable', 
            hue='prediction', orient="h")

Which works great enter image description here

However, I want the bars to be red. Below:

sns.barplot(data=x, x='value', y='variable', 
            hue='prediction', color='red', orient="h")

Results in: enter image description here

Why does it turn gray? I have tried multiple other colors as well with the same result. How can I turn the bars orange?

1 Answers

Using seaborn v0.11.1

Try using facecolor:

sns.barplot(data=x, x='value', y='variable', 
            hue='prediction', orient="h", facecolor='red')

Output:

enter image description here

  • color is used for all elements or a seed for a gradient palette, while edgecolor (ec), and facecolor (fc) specify separate elements.
    • ec and fc take precedence over hue, and color.
  • Using color with hue, colors the bars based on a gradient, however since 'prediction' has only one value, the gradient starts at grey.
import pandas as pd
import seaborn as sns

# new data; note there are different values for prediction
{'prediction': {5: 'c1', 4: 'c3', 3: 'c3', 2: 'c1', 0: 'c1', 1: 'c2', 7: 'c2', 6: 'c2'}, 'variable': {5: 'ft1', 4: 'ft2', 3: 'ft3', 2: 'ft4', 0: 'ft5', 1: 'ft6', 7: 'ft7', 6: 'ft8'}, 'value': {5: 0.020915912763961077, 4: 0.020388363414781133, 3: 0.007204373035913109, 2: 0.0035298765062560817, 0: -0.002049702058734183, 1: -0.004283512505036808, 7: -0.01882610282871816, 6: -0.022324439779323434}}

df = pd.DataFrame(x)

sns.barplot(data=df, x='value', y='variable', hue='prediction', orient="h", color='red', ec='k', dodge=False)

enter image description here

Note that dodge=False is used to prevent bars being shifted along the categorical axis, which happens when using hue.

Related