Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to plot several barplots using seaborn with respect to row?

Let’s consider the data following:

accuracies_in = ([0.5959219858156029, 0.5736842105263158, 0.5670212765957447, 0.3])
accuracies_out = [0.5, 0.6041666666666666, 0.2, 0.4]
auc_out = [0.5182608695652174, 0.6095652173913042, 0.5, 0.7]
algorithm = ["Logistic Regression", "Decision Tree", "Random Forest", "Neural Network"]
frame = pd.DataFrame([accuracies_in, accuracies_out, auc_out])
frame.index = ["accuracies_in", "accuracies_out", "auc_out"]
frame.columns = algorithm

enter image description here

I want to have a barplot which will show results for those three characteristics (accuracies_in, accuarcies_out and auc_out). In other words on the barplot I want to group barplots more or less like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

The first group (first row – accuracy_in) will contain four bars – accuracies_in for Logistic Regression, Decision Tree, Random Forest and Neural Network. Second group of bars will contain again four bars but this time for second row – accuracies_out. And lastly – the third group of bars will contain four bars with auc_out in each of them.

Could you please help me how it can be done? I searched a lot of questions but I couldn’t find anything which will plot barplots with respect to rows. Could you please give me a hand in this problem?

>Solution :

You need to convert your dataframe to long form, using pd.melt.

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

# frame: given dataframe

df = frame.melt(var_name='Algorithm', value_name='Accuracy', ignore_index=False)
sns.set()
sns.barplot(data=df, x=df.index, y='Accuracy', hue='Algorithm')

sns.barplot from long dataframe

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading