2016-04-24 12 views
0

私はデータフレーム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 

DF- 私はこのデータをプロットする水平barplotを作成したいです。

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

しかし、上記のコード部分は与え、エラー - TypeError: cannot convert the series to <class 'int'>

どのように私は、水平バープロットを作成することができますか?

私は、Excelで予想プロットにプロットしている - 私はPythonでこれを再作成するにはどうすればよい enter image description here

を?

+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

私のためにも:)あなたのデータを使って遊んでいたら、おそらくdictや配列に入れてmatplotlibに渡す必要があります – Ryan

0

plt.barh()の二番目の引数は、数値でなければならず、expense['Source']があるようにそれは見えません。あなたがバーの幅をどのようにしたいかわからないので、私はもっと具体的な答えを出すことはできませんが、明らかに "University of Minnesota"は有効な幅ではありません。

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 

、興味深い部分、第1の使用データフレームメソッドデータフレームコンテンツをプロットする、

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

上記ラベルy軸0、1、2、いいえ...というようにプロットされたオブジェクトを修正する必要があるので、まずプロットされたオブジェクトを取得しなければなりませんトン(現在の軸を得るためgcaスタンドは)

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

その後、あなたはそれはそれはオブジェクト指向だ、されていません教えて?あなたはそれが(当然)

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

In [9]: 

で、yはラベルを刻む修正するために、現在の軸に伝え、これは出力

enter image description here

2

私はあなたがplot.barhを使用することができると思うが、とset_indexrename_axispandas0.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

関連する問題