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" 
martineau
  • 119,623
  • 25
  • 170
  • 301
SophieMQ
  • 55
  • 7
  • 2
    Look here: https://stackoverflow.com/questions/55567706/plot-all-pandas-dataframe-columns-separately – NumberC Jul 07 '20 at 23:37
  • Does this answer your question? [Plot all pandas dataframe columns separately](https://stackoverflow.com/questions/55567706/plot-all-pandas-dataframe-columns-separately) – NumberC Jul 07 '20 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. – SophieMQ Jul 07 '20 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.. – SophieMQ Jul 08 '20 at 00: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. – Mark Andersen Jul 08 '20 at 02:16
  • Did you try `df.plot(kind='bar')` ? Is this what you're looking for? – Julien Roullé Jul 08 '20 at 02:20

1 Answers1

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

Nilesh Ingle
  • 1,777
  • 11
  • 17