I'm trying to plot a bar chart to represent the frequency of two variables (Dead and Alive) in my test and training data. I want it to look like the second example described in the documentation (minus the error bars).

Image showing desired output

Currently i have

yfreq = pd.DataFrame(data = [Counter(Ytrain),Counter(Ytest)],
                     index = ['Train','Test'])
yfreq.columns = ['Dead', 'Alive']
>>>        Dead  Alive
    Train   384    239
    Test    165    103

And then for the graph

sns.barplot(data = yfreq)

Which correctly outputs a graph but does not split the data into test and training data.

Image showing Currently outputted graph

I know it has something to do with the hue kwarg but i can't work out how too do it.

Thanks in advance


This should do the trick. You have to melt your data frame to use x,y and hue in your seaborn barplot.

yfreq['type'] = yfreq.index
yfreq = yfreq.melt(id_vars = 'type')
sns.barplot(x = 'variable', y = 'value', hue = 'type', data = yfreq)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.