Caledonian26
Caledonian26

Reputation: 809

Bar plot not appearing normally using df.plot.bar()

I have the following code. I am trying to loop through variables (dataframe columns) and create bar plots. I have attached below an example of a graph for the column newerdf['age'].

I believe this should produce 3 bars (one for each option - male (value = 1), female (value = 2), other(value = 3)).

However, the graph below does not seem to show this.

I would be so grateful for a helping hand as to where I am going wrong!

listedvariables = ['age','gender-quantised','hours_of_sleep','frequency_of_alarm_usage','nap_duration_mins','frequency_of_naps','takes_naps_yes/no','highest_education_level_acheived','hours_exercise_per_week_in_last_6_months','drink_alcohol_yes/no','drink_caffeine_yes/no','hours_exercise_per_week','hours_of_phone_use_per_week','video_game_phone/tablet_hours_per_week','video_game_all_devices_hours_per_week']
for i in range(0,len(listedvariables)): 
    fig = newerdf[[listedvariables[i]]].plot.bar(figsize=(30,20))
    fig.tick_params(axis='x',labelsize=40)
    fig.tick_params(axis='y',labelsize=40)
    plt.tight_layout()
newerdf['age']

    age
0   2
1   2
2   4
3   3
5   2
... ...
911 2
912 1
913 2
914 3
915 2

enter image description here

Upvotes: 0

Views: 84

Answers (1)

Arne
Arne

Reputation: 10545

The data are not grouped into categories yet, so a value count is needed before calling the plotting method:

for var in listedvariables: 
    ax = newerdf[var].value_counts().plot.bar(figsize=(30,20))
    ax.tick_params(axis='x', labelsize=40)
    ax.tick_params(axis='y', labelsize=40)
    plt.tight_layout()
    plt.show()

Upvotes: 1

Related Questions