簡體   English   中英

Python 3.x-水平條形圖

[英]Python 3.x - Horizontal bar plot

我有一個數據框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

我想創建一個水平條形圖來繪制此數據。

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

但是上面的代碼給出了錯誤TypeError: cannot convert the series to <class 'int'>

如何創建水平條形圖?

我已經在excel中繪制了預期的圖- 在此處輸入圖片說明

我如何在python中重新創建它?

我認為您可以使用plot.barh ,但是在set_index之前使用rename_axis (在pandas新的0.18.0 )和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()

圖形

樣板

In [1]: import matplotlib.pyplot as plt

In [2]: %matplotlib
Using matplotlib backend: Qt4Agg

In [3]: import pandas as pd

我的虛假數據

In [4]: data = pd.read_csv('data.csv')

In [5]: data
Out[5]: 
       Name   Value
0  asde rty     100
1   4 wewer     200
2   uwei ef     300

現在,有趣的部分是,首先使用數據框方法繪制數據框內容,

In [6]: data.plot.barh()
Out[6]: <matplotlib.axes._subplots.AxesSubplot at 0x7facb0706198>

上面的y軸標記為0、1、2不好...所以我們必須修改繪制的對象,首先必須抓住繪制的對象( gca代表獲取當前軸)

In [7]: ax = plt.gca()

然后你說,它是面向對象的,不是嗎? 您告訴當前軸修改y刻度標簽,即(毫不奇怪)

In [8]: ax.set_yticklabels(data['Name']);
Out[8]: 

In [9]: 

這是輸出

在此處輸入圖片說明

我可能會為此感到尷尬,但這是否可能是您需要將其他類型的數據提供給matplotlib?

import matplotlib.pyplot as plt
expense_df = {'Amount' : 0, 'Amount' : 1, 'Amount' : 2}
expense = {'Source' : 1, 'Source' : 2, 'Source' : 3}

plt.barh(expense_df['Amount'],expense['Source'])
plt.show()

plt.barh()的第二個參數必須是數字,並且看起來不像是expense['Source'] 我無法提供更具體的答案,因為我不知道您希望條形的寬度是多少,但是顯然“明尼蘇達大學”不是有效的寬度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM