2016-04-24 20 views
0

我有一個數據幀DF - DF-的Python 3.X - 單槓情節

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 

我想創建一個水平barplot繪製該數據。

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

但上面這段代碼提供了和錯誤 - TypeError: cannot convert the series to <class 'int'>

如何創建一個水平條形圖?

我已經繪製在Excel中的預期情節 - enter image description here

我怎麼能在Python重建呢?

+0

您發佈的數據,是不是假的,公開或保密的嗎? – gboffi

+0

公共數據。 source- https://www.revisor.mn.gov/laws/?year=2014&type=0&doctype=Chapter&id=294 –

回答

0

我可能會在這裏感到尷尬,但可能是因爲您需要將不同類型的數據輸入到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() 
+0

感謝@Ryan的回覆,但只爲我繪製了空白藍屏 –

+0

也有:)有點玩弄你的數據,你可能必須把它放在一個字典或一個數組,然後傳遞給matplotlib – Ryan

0

的第二個參數plt.barh()必須是數字,它看起來並不像expense['Source']是。我不能提供更具體的答案,因爲我不知道你想要什麼酒吧的寬度,但顯然「明尼蘇達大學」不是一個有效的寬度。

1

樣板

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,沒有好處...所以我們必須修改繪製的對象,首先你必須抓住繪製的對象T(gca代表獲得當前軸)

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

那麼你知道的,它是面向對象的,不是嗎?你告訴當前軸修改Ÿ蜱標籤,即(意料之中)

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

In [9]: 

,這是輸出

enter image description here

2

我認爲你可以使用plot.barh,但set_index與前rename_axis(新中pandas0.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() 

graph