Python 3.x-水平条形图 [英] Python 3.x - Horizontal bar plot

查看:34
本文介绍了Python 3.x-水平条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框df-df-

 源金额1明尼苏达大学119367000明尼苏达州2所州立大学1598120003教育74910004明尼苏达州立学院113540005 Perpich艺术教育中心20000006自然资源634800007污染控制局26250008水土资源局80000009农业20300010动物园1200000011行政管理12700000012明尼苏达州业余运动委员会797300013军事324400014公共安全403000015运输5726300016大都会议会4596800017人员服务8638700018退伍军人事务280000019更正1188100020就业与经济发展9213000021公共设施管理局4599300022房屋金融局2000000023明尼苏达州历史学会1200200024债券销售费用90000025取消-1084900026合计89305400027债券收益基金(普通基金债务服务)81474500028债券收益基金(用户融资债务服务)3910400029国家交通基金3661300030尽最大努力学校贷款基金549100031干线公路基金795000032债券收益取消-10849000 

我想创建一个水平条形图来绘制此数据.

 将matplotlib.pyplot导入为pltplt.barh(expense_df ['Amount'],expense ['Source'])plt.show() 

但是上面的代码给出了错误- TypeError:无法将序列转换为< class'int'>

如何创建水平条形图?

我已经在excel中绘制了预期的图-

如何在python中重新创建它?

解决方案

我认为您可以使用

I have a dataframe df - df-

    Source  Amount
1   University of Minnesota 119367000
2   Minnesota State Colleges and Universities   159812000
3   Education   7491000
4   Minnesota State Academies   11354000
5   Perpich Center for Arts Education   2000000
6   Natural Resources   63480000
7   Pollution Control Agency    2625000
8   Board of Water and Soil Resources   8000000
9   Agriculture 203000
10  Zoological Garden   12000000
11  Administration  127000000
12  Minnesota Amateur Sports Commission 7973000
13  Military Affairs    3244000
14  Public Safety   4030000
15  Transportation  57263000
16  Metropolitan Council    45968000
17  Human Services  86387000
18  Veterans Affairs    2800000
19  Corrections 11881000
20  Employment and Economic Development 92130000
21  Public Facilities Authority 45993000
22  Housing Finance Agency  20000000
23  Minnesota Historical Society    12002000
24  Bond Sale Expenses  900000
25  Cancellations   -10849000
26  TOTAL   893054000
27  Bond Proceeds Fund (General Fund Debt Servic... 814745000
28  Bond Proceeds Fund (User Financed Debt Servi... 39104000
29  State Transportation Fund   36613000
30  Maximum Effort School Loan Fund 5491000
31  Trunk Highway Fund  7950000
32  Bond Proceeds Cancellations -10849000

I want to create a horizontal barplot to plot this data.

import matplotlib.pyplot as plt
plt.barh(expense_df['Amount'],expense['Source'])
plt.show()

But the above piece of code gives and error - TypeError: cannot convert the series to <class 'int'>

How can i create a horizontal bar plot?

I have plotted the expected plot in excel -

How can i recreate this in python?

解决方案

I think you can use plot.barh, but before set_index with rename_axis (new in pandas 0.18.0) and sort_values:

#set index from column Source, remove index name
df = df.set_index('Source').rename_axis(None)
#sorting values
df = df.sort_values('Amount', ascending=False)

print df
                                                    Amount
TOTAL                                            893054000
Bond Proceeds Fund (General Fund Debt Service)   814745000
Minnesota State Colleges and Universities        159812000
Administration                                   127000000
University of Minnesota                          119367000
Employment and Economic Development               92130000
Human Services                                    86387000
Natural Resources                                 63480000
Transportation                                    57263000
Public Facilities Authority                       45993000
Metropolitan Council                              45968000
Bond Proceeds Fund (User Financed Debt Service)   39104000
State Transportation Fund                         36613000
Housing Finance Agency                            20000000
Minnesota Historical Society                      12002000
Zoological Garden                                 12000000
Corrections                                       11881000
Minnesota State Academies                         11354000
Bond Proceeds Cancellations                       10849000
Cancellations                                     10849000
Board of Water and Soil Resources                  8000000
Minnesota Amateur Sports Commission                7973000
Trunk Highway Fund                                 7950000
Education                                          7491000
Maximum Effort School Loan Fund                    5491000
Public Safety                                      4030000
Military Affairs                                   3244000
Veterans Affairs                                   2800000
Pollution Control Agency                           2625000
Perpich Center for Arts Education                  2000000
Bond Sale Expenses                                  900000
Agriculture                                         203000

df.plot.barh(figsize=(10,20))
plt.show()

这篇关于Python 3.x-水平条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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