Pandas - 绘制堆积条形图 [英] Pandas - Plotting a stacked Bar Chart

查看:82
本文介绍了Pandas - 绘制堆积条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个复制图片的堆叠条形图,我的所有数据都与该 Excel 电子表格分开.

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

我无法弄清楚如何为它制作如图所示的数据框,也无法弄清楚如何制作堆积条形图.我找到的所有示例都以与我尝试创建的方式不同的方式工作.

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.

我的数据框是一个包含所有值的 csv,使用熊猫数据框缩小到以下范围.

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)

推荐答案

您是否遇到错误,或者只是不确定从哪里开始?

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)

这篇关于Pandas - 绘制堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆