删除总计分组并从结果中创建堆积的条形图

时间:2019-06-03 21:19:27

标签: python dataframe pandas-groupby

我需要根据数据分组创建堆叠的条形图。我只希望图表上总计较高的组。我能否获得修改组和堆积条形图的帮助。

我能够找到需要删除的组,并且能够基于分组创建条形图。

import matplotlib.pyplot as plt
import pandas as pd

#csv holds 3 columns App Name, Region Number, District Number (if applicable), # regs holds unique region numbers, apps holds unique app names
fig= plt.figure(figsize=(9,6))
d = pd.read_csv('xxx/licenses.csv')
regs = d.Region.unique()
apps = d.AppName.unique()

#df3 will end up holding the correct list of app names. I only want to chart
# the apps that have over 5 total.
df = d.groupby(['AppName']).size().reset_index(name='counts')
df2  = df['counts'] > 5
df3=df[df2]

#This appropriately groups and graphs the groupings, but includes all 
# applications, not the the ones with totals > 5
counts = d.groupby(['AppName','Region']).count()
totals = counts.sum(level=0)
counts = counts.unstack(level=1)
counts.columns = counts.columns.droplevel(level=0)
counts = counts.fillna(0)

#all bars are on top of each other. need to calculate the bottom for each 
# iteration. 
# x axis should have app names, y should be count of districts in region, 
# with each region its own color to see which regions are using which app
for reg in regs:
    plt.bar(apps, counts[reg], bottom=None)
plt.show()

条形图相互堆叠,但所有条形图均从0开始。它们应从上一个的最大值开始。

仅应绘制总数大于5的应用程序

0 个答案:

没有答案