Home > Software design >  python - set x axis as column names on barplot
python - set x axis as column names on barplot

Time:11-18

I have a dataframe such as this:

     name   100m    200m   400m   800m
1     Bob     19      36     67    117
2   Chuck     14      25     64    120
3   Daren     12      24     58    123
4   Elisa     11      24     57    121

My task is simple: Plot the times (along the y-axis), with the name of the event (100m, 200m, etc. along the x-axis). The hue of each bar should be determined by the 'name' column, and look something like this.

column names as x-axis

Furthermore, I would like to stack the results. However, there is no functionality in seaborn nor matplotlib to do this.

Any help is appreciated in helping me.

CodePudding user response:

Try this:

df.set_index('name').T.plot.bar(alpha=.7, rot=0)

Output:

enter image description here

CodePudding user response:

df.plot(kind='bar', stacked=True)

This should work, you can also choose colours using

colors=[col1, col2, col3] as a parameter of plot
  • Related