0

I have a dataframe with the accuracy results of different ML models.

In the columns I have the different ML models, and in the rows I have the results of the accuracy of these ML models after different experiments.

Something like this:

dataframe for reference

I want to print a barplot in which I can see in the x axis the different ML models, in the y axis the results of accuracy, and inside every ML model in x axis, different bars for every row in the dataframe.

My logic says that it should be something as follows:

sns.barplot(data=df, x=df.columns, y=???, hue=df.index)

How do I indicate that in the y axis? I want to show the different values of rows.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • 1
    Please read [Why should I not upload images of code/data/errors?](https://meta.stackoverflow.com/q/285551/354577) (this applies to data as well). Instead, format code as a [code block]. The easiest way to do this is to paste the code as text directly into your question, then select it and click the code block button. – ChrisGPT was on strike Apr 09 '23 at 12:08
  • Create a new column called `'Test'` from the index numbers, and melt the dataframe into a long form with `dfl = df.assign(Test=df.index).melt(id_vars='Test', var_name='Model', value_name='Accuracy')`, then plot with `ax = sns.barplot(data=dfl, x='Model', y='Accuracy', hue='Test')`. See [code and plot](https://i.stack.imgur.com/RghOI.png). Or use `.assign(Test=range(1, len(df)+1))` if you want to start from Test 1 instead of 0. – Trenton McKinney Apr 09 '23 at 17:19
  • More expeditiously, `ax = df.T.plot(kind='bar', figsize=(10, 7))` – Trenton McKinney Apr 09 '23 at 23:35

1 Answers1

0
import seaborn as sns
import pandas as pd

df = pd.DataFrame([
    ["model family 1", "model 1", 0.2],
    ["model family 1", "model 2", 0.1],
    ["model family 2", "model 1", 0.5],
    ["model family 2", "model 2", 0.3],
], columns=["model family", "model name", "accuracy"])

sns.barplot(df, x="model family", y="accuracy", hue="model name")

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Klops
  • 951
  • 6
  • 18