-1
    model_name  1   2   3   4   5
0   ground_truth    41.333333   12.000000   12.666667   11.333333   22.666667
1   TL_model    73.333333   4.666667    2.666667    4.666667    14.666667
2   other-model 22.000000   21.333333   9.333333    31.333333   16.000000

Assume I have the above dataframe. I'm trying to achieve the following:

  1. I would like the x-axis to be [1,2,3,4,5]
  2. I would like the y-axis to reflect the frequency of each entry in the dataframe, for example entry [ground_truth][1] -> 41.333 and so on
  3. I would like to have 3 bars next to each other for each entry in the x-axis (15 bars in total). That is for x-value 1, I would like the first bar to reflect ground_truth frequency-> 41.333, 2nd value to reflect TL_model's frequency -> 73.3333; and the last value to reflect other-model's frequency -> 22.0000. So that would be 3 bars for value 1, 3 bars for 2,.... all the way to value 5.

How do I do this using Seaborn? I'm struggling quite a lot and can't figure out how to set the "hue" parameter without setting the x and y axis properly. But I'm not sure how to set the y-axis in this case.

Any help would be greatly appreciated.

skidjoe
  • 493
  • 7
  • 16

1 Answers1

0

Example

data = {'model_name': {0: 'ground_truth', 1: 'TL_model', 2: 'other-model'},
 '1': {0: 41.333333, 1: 73.333333, 2: 22.0},
 '2': {0: 12.0, 1: 4.666667, 2: 21.333333},
 '3': {0: 12.666667, 1: 2.666667, 2: 9.333333},
 '4': {0: 11.333333, 1: 4.666667, 2: 31.333333},
 '5': {0: 22.666667, 1: 14.666667, 2: 16.0}}
df = pd.DataFrame(data)

Code

Your problem can be solved in many ways. Since dataframe that draws graph is not large data, using hue by unpivoting is one of the convenient ways. lets melt for unpivot.

sns.barplot(data=df.melt('model_name'), x='variable', y='value', hue='model_name')

output:

enter image description here

Panda Kim
  • 6,246
  • 2
  • 12