任务11:叠加柱状图,水平柱状图
在DataFrame里面新增加一列“Total”:
users['Total'] = users['Oct'] + users['Nov'] + users['Dec']
生成叠加柱状图:
users.plot.bar(x='Name', y=['Oct', 'Nov', 'Dec'], stacked=True, title='User Behavior')
注:Stached=True
生成水平柱状图:
users.plot.barh(x='Name', y=['Oct', 'Nov', 'Dec'], stacked=True, title='User Behavior')
注:users.plot.barh
本节代码:
import pandas as pd
import matplotlib.pyplot as plt
users = pd.read_excel('C:/Temp/Users.xlsx')
users['Total'] = users['Oct'] + users['Nov'] + users['Dec']
users.sort_values(by='Total', inplace=True, ascending=True)
print(users)
users.plot.barh(x='Name', y=['Oct', 'Nov', 'Dec'], stacked=True, title='User Behavior')
plt.tight_layout()
plt.show()
打印结果
ID Name Oct Nov Dec Total
11 12 User_012 7 11 11 29
13 14 User_014 9 11 11 31
0 1 User_001 7 15 10 32
1 2 User_002 10 13 10 33
5 6 User_006 11 9 14 34
9 10 User_010 9 15 10 34
3 4 User_004 8 14 13 35
17 18 User_018 8 13 14 35
4 5 User_005 8 11 16 35
19 20 User_020 9 13 13 35
7 8 User_008 9 12 16 37
8 9 User_009 11 11 15 37
10 11 User_011 9 14 15 38
18 19 User_019 8 16 15 39
2 3 User_003 12 10 17 39
16 17 User_017 10 13 16 39
12 13 User_013 7 15 18 40
6 7 User_007 11 14 17 42
14 15 User_015 10 15 18 43
15 16 User_016 12 14 17 43
end.