Pandas - Plotting a stacked Bar Chart

泪湿孤枕 提交于 2019-11-26 09:28:34

问题


I am trying to create a stacked bar graph that replicates the picture, all my data is separate from that excel spreadsheet.

\"enter

I cant figure out how to make a dataframe for it like pictured, nor can I figure out how to make the stacked bar chart. All examples I locate work in different ways to what I\'m trying to create.

My dataframe is a csv of all values narrowed down to the following with a pandas dataframe.

      Site Name    Abuse/NFF
0    NORTH ACTON       ABUSE
1    WASHINGTON         -
2    WASHINGTON        NFF
3    BELFAST            -
4    CROYDON            - 

I have managed to count the data with totals and get individual counts for each site, I just cant seem to combine it in a way to graph.

Would really appreciate some strong guidance.

Completed code, many thanks for the assistance completing.

test5 = faultdf.groupby([\'Site Name\', \'Abuse/NFF\'])[\'Site Name\'].count().unstack(\'Abuse/NFF\').fillna(0)

test5.plot(kind=\'bar\', stacked=True)

回答1:


Are you getting errors, or just not sure where to start?

%pylab inline
import pandas as pd
import matplotlib.pyplot as plt

df2 = df.groupby(['Name', 'Abuse/NFF'])['Name'].count().unstack('Abuse/NFF').fillna(0)
df2[['abuse','nff']].plot(kind='bar', stacked=True)




回答2:


That should help

df.groupby(['NFF', 'ABUSE']).size().unstack().plot(kind='bar', stacked=True)



回答3:


Maybe you can use pandas crosstab function

test5 = pd.crosstab(index=faultdf['Site Name'], columns=faultdf[''Abuse/NFF''])

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



回答4:


If you want to change the size of plot the use arg figsize

df.groupby(['NFF', 'ABUSE']).size().unstack()
      .plot(kind='bar', stacked=True, figsize=(15, 5))


来源:https://stackoverflow.com/questions/23415500/pandas-plotting-a-stacked-bar-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!