2

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:

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?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
John
  • 1,849
  • 2
  • 13
  • 23

3 Answers3

3

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

JohanC
  • 71,591
  • 8
  • 33
  • 66
1

You really have almost nothing to do but using the pandas builtin plot.bar:

frame.plot.bar()

input frame:

                Logistic Regression  Decision Tree  Random Forest  Neural Network
accuracies_in              0.595922       0.573684       0.567021             0.3
accuracies_out             0.500000       0.604167       0.200000             0.4
auc_out                    0.518261       0.609565       0.500000             0.7

output:

enter image description here

mozway
  • 194,879
  • 13
  • 39
  • 75
0

What you can use is something like:

X = frame.index

X_axis = np.arange(len(X))
dist = 0.2
plt.figure(figsize=(10,8))
plt.bar(X_axis + (-1)* dist, frame["Logistic Regression"], dist, label = 'accuracies_in')
plt.bar(X_axis + 0 * dist, frame["Decision Tree"], dist, label = 'accuracies_out')
plt.bar(X_axis + 1 * dist, frame["Random Forest"], dist, label = 'auc_out')
plt.bar(X_axis + 2 * dist, frame["Neural Network"], dist, label = 'auc_out')
  
plt.xticks(X_axis, X)
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.legend()
plt.show()

Output

Your desired plot

TheFaultInOurStars
  • 3,464
  • 1
  • 8
  • 29