0

I have a dataframe with 45 columns (see below). I would like to plot each column as a separate bar graph. I'm very new to Python and can't figure out how to do this. I think the problem might be the way the dataframe is set up with the row names.

        V1_category V2_category V3_category V4_category V5_category V6_category
Neutral 78.378378   83.783784   59.459459   27.027027   54.054054   32.432432
Painful 0.000000    0.000000    0.000000    2.702703    2.702703    13.513514   
Pleasant8.108108    10.810811   2.702703    16.216216   5.405405    2.702703
Unpleasant13.513514 5.405405    37.837838   54.054054   37.837838   51.351351" 
6
  • 2
    Look here: stackoverflow.com/questions/55567706/… Commented Jul 7, 2020 at 23:37
  • Does this answer your question? Plot all pandas dataframe columns separately Commented Jul 7, 2020 at 23:37
  • I've tried all those suggestions several times but none of them are working. Either it creates very strange graphs or the x and y-axis values are very strange and it is definitely not showing the actual data. Commented Jul 7, 2020 at 23:58
  • I've done it in R where I wrote a loop to go through each column and create a temporary data frame for the column values and plot it. But I feel like there should be a much easier way where I just directly plot each column.. Commented Jul 8, 2020 at 0:00
  • It might help to show a picture of what you are looking for? 45x4 seems like a lot of bars to make visual distinction. Sometimes one might use subplots (see Seaborn for example). And I wonder if you are actually asking for 45 stacked bars. A sample picture you generate from Excel could indicate your goal. Commented Jul 8, 2020 at 2:16

1 Answer 1

1

One way could be:

# Import libraries
import pandas as pd
import matplotlib.pyplot as plt

# Create DataFrame
df = pd.DataFrame({
    'V1_category': [78.378378,0.0,8.108108,13.513514],
    'V2_category': [83.783784,0.0,10.810811,5.405405],
    'V3_category': [59.459459,0.0,2.702703,37.837838],
    'V4_category': [27.027027,2.702703,16.216216,54.054054],
    'V5_category': [54.054054,2.702703,5.405405,37.837838],
    'V6_category': [32.432432, 13.513514,2.702703, 51.351351]
    }, index =  ['Neutral','Painful','Pleasant','Unpleasant']
)
df

# Using pandas 'df.plot'
for col in df.columns:
    df[col].plot(kind='bar')
    plt.title(col)
    plt.show()

Only two plots shown below:

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.