0

I searched through this forum and unfortunatelly I do not know what am I doing wrong. I have three .csv files file1.csv

name     value     type
model1   100.1     all
model2   121.1     all
model3   151.1     all

file2.csv

name     value     type
model1   110.1     one
model2   131.1     one
model3   141.1     one
model4   161.1     one
model5   171.1     one

file3.csv

name     value     type
model1   80.1      two
model2   85.1      two
model3   90.1      two
model4   92.1      two
model5   94.1      two

and i want to make plot which shows all three 'value' bars for each 'name' - thats why I add "type" column.

So I made dataframe from each .csv file and named them df_file1, df_file2, df_file3. So I tried to make a plot:

df_all = pd.concat([df_file1, df_file2, df_file3])
ax = (
     df_all
).plot(kind="bar", rot=45, figsize=(20,6), grid = True, color=['r', 'g', 'y'])

for container in ax.containers:
    ax.bar_label(container)

but unfortunatelly my plot shows three bars for df_file1, five bars for df_file2, five bars for df_file3 instead of three bars for model1, three bars for model2,three bars for model3,three bars for model4 and three bars for model5 with each bar in different column

Is there any way to make a plot just as I disribed it?

1
  • 1
    df_all.pivot('name', 'type', 'value').plot(kind='bar') ..? Commented Jul 4, 2022 at 13:15

1 Answer 1

1

IIUC, try pivoting your data first, so that each column represents a type and each row represents your name:

df_piv = df_all.pivot('name', 'type', 'value')

[out]

type      all    one   two
name                      
model1  100.1  110.1  80.1
model2  121.1  131.1  85.1
model3  151.1  141.1  90.1
model4    NaN  161.1  92.1
model5    NaN  171.1  94.1

Then the plotting command you were using:

df_piv.plot(kind='bar', rot=45, figsize=(20,6), grid=True, color=['r', 'g', 'y'])

[out]

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.