In [212]:
import plotly
plotly.tools.set_credentials_file(username='yuxiangling0809', api_key='vJxzgz9EZWkJZdHur9A8')
In [213]:
import plotly
plotly.tools.set_config_file(world_readable=True,
sharing='public')
In [214]:
import plotly.plotly as py
In [215]:
import pandas as pd
In [27]:
df = pd.read_csv('/Users/qiqi/DV590/Administrative_Discretionary_Grants_Dataset.csv')
In [80]:
df1 = pd.read_csv('/Users/qiqi/DV590/Part2-Q3-data.csv')
In [81]:
df.head()
Out[81]:
In [82]:
df1.head()
Out[82]:
In [83]:
df['text'] = df['Program'] +'<br>'+ 'Total Amount Awarded and Disbursed ' + (df['Total Amount Awarded and Disbursed']/1e3).astype(str)+'thousand'+'<br>'
limits = [(0,10),(10,100),(100,200),(200,500),(500,1000),(1000,3000)]
colors = ["lightgrey","rgb(255,65,54)","rgb(133,20,75)","rgb(255,133,27)","#ffb266","rgb(0,116,217)"]
Programs = []
scale = 6000
In [84]:
for i in range(len(limits)):
lim = limits[i]
df_sub = df[((df['Total Amount Awarded and Disbursed']/1e3)<limits[i][1])&((df['Total Amount Awarded and Disbursed']/1e3)>limits[i][0])]
program = dict(
type = 'scattergeo',
locationmode = 'USA-states',
lon = df_sub['LNG'],
lat = df_sub['LAT'],
text = df_sub['text'],
marker = dict(
size = df_sub['Total Amount Awarded and Disbursed']/scale,
color = colors[i],
line = dict(width=0.5, color='rgb(40,40,40)'),
sizemode = 'area'
),
name = '{0} - {1}'.format(limits[i][0],limits[i][1]) )
Programs.append(program)
In [85]:
trace1=Programs
In [86]:
scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],
[0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]
In [132]:
trace2 = [ dict(
type='choropleth',
colorscale = scl,
autocolorscale = False,
locations = df1['locations'],
z = [156750,40063,227358,90319,1766693,220454,210170,53247,83586,700267,388342,58573,47176,586442,245197,123353,104884,143875,199683,46277,263809,345059,395166,243933,81678,224091,30637,73455,118785,57282,443148,74403,1023433,357168,24622,467392,125243,147535,506505,45663,143585,31380,227505,990054,94475,23539,359273,296403,53453,226325,27454],
locationmode = 'USA-states',
text = ["State: AL<br>Total Awarded Amount: 5743215<br>GDP(million): 156750",
"State: AK<br>Total Awarded Amount: 16189749<br>GDP(million): 40063",
"State: AZ<br>Total Awarded Amount: 19037687<br>GDP(million): 227358",
"State: AR<br>Total Awarded Amount: 1965330<br>GDP(million): 90319",
"State: CA<br>Total Awarded Amount: 74663752<br>GDP(million): 1766693",
"State: CO<br>Total Awarded Amount: 22235728<br>GDP(million): 220454",
"State: CT<br>Total Awarded Amount: 16501999<br>GDP(million): 210170",
"State: DE<br>Total Awarded Amount: 3857523<br>GDP(million): 53247",
"State: DC<br>Total Awarded Amount: 25424106<br>GDP(million): 83586",
"State: FL<br>Total Awarded Amount: 26600593<br>GDP(million): 700267",
"State: GA<br>Total Awarded Amount: 11216327<br>GDP(million): 388342",
"State: HI<br>Total Awarded Amount: 14427899<br>GDP(million): 58573",
"State: ID<br>Total Awarded Amount: 1961349<br>GDP(million): 47176",
"State: IL<br>Total Awarded Amount: 68112505<br>GDP(million): 586442",
"State: IN<br>Total Awarded Amount: 16377153<br>GDP(million): 245197",
"State: IA<br>Total Awarded Amount: 8493716<br>GDP(million): 123353",
"State: KS<br>Total Awarded Amount: 5764416<br>GDP(million): 104884",
"State: KY<br>Total Awarded Amount: 8022942<br>GDP(million): 143875",
"State: LA<br>Total Awarded Amount: 7978539<br>GDP(million): 199683",
"State: ME<br>Total Awarded Amount: 11177079<br>GDP(million): 46277",
"State: MD<br>Total Awarded Amount: 21671260<br>GDP(million): 263809",
"State: MA<br>Total Awarded Amount: 45976945<br>GDP(million): 345059",
"State: MI<br>Total Awarded Amount: 28799226<br>GDP(million): 395166",
"State: MN<br>Total Awarded Amount: 15325162<br>GDP(million): 243933",
"State: MS<br>Total Awarded Amount: 2787779<br>GDP(million): 81678",
"State: MO<br>Total Awarded Amount: 14911587<br>GDP(million): 224091",
"State: MT<br>Total Awarded Amount: 10424194<br>GDP(million): 30637",
"State: NE<br>Total Awarded Amount: 6600356<br>GDP(million): 73455",
"State: NV<br>Total Awarded Amount: 3677624<br>GDP(million): 118785",
"State: NH<br>Total Awarded Amount: 4797223<br>GDP(million): 57282",
"State: NJ<br>Total Awarded Amount: 11475362<br>GDP(million): 443148",
"State: NM<br>Total Awarded Amount: 10640574<br>GDP(million): 74403",
"State: NY<br>Total Awarded Amount: 105860170<br>GDP(million): 1023433",
"State: NC<br>Total Awarded Amount: 29992038<br>GDP(million): 357168",
"State: ND<br>Total Awarded Amount: 3071608<br>GDP(million): 24622",
"State: OH<br>Total Awarded Amount: 22015009<br>GDP(million): 467392",
"State: OK<br>Total Awarded Amount: 12405823<br>GDP(million): 125243",
"State: OR<br>Total Awarded Amount: 9556529<br>GDP(million): 147535",
"State: PA<br>Total Awarded Amount: 59575053<br>GDP(million): 506505",
"State: RI<br>Total Awarded Amount: 4751919<br>GDP(million): 45663",
"State: SC<br>Total Awarded Amount: 9063478<br>GDP(million): 143585",
"State: SD<br>Total Awarded Amount: 3596551<br>GDP(million): 31380",
"State: TN<br>Total Awarded Amount: 18083590<br>GDP(million): 227505",
"State: TX<br>Total Awarded Amount: 48252344<br>GDP(million): 990054",
"State: UT<br>Total Awarded Amount: 6884351<br>GDP(million): 94475",
"State: VT<br>Total Awarded Amount: 6381133<br>GDP(million): 23539",
"State: VA<br>Total Awarded Amount: 18068081<br>GDP(million): 359273",
"State: WA<br>Total Awarded Amount: 23476750<br>GDP(million): 296403",
"State: WV<br>Total Awarded Amount: 927680<br>GDP(million): 53453",
"State: WI<br>Total Awarded Amount: 17075827<br>GDP(million): 226325",
"State: WY<br>Total Awarded Amount: 2210361<br>GDP(million): 27454"],
marker = dict(
line = dict (
color = 'rgb(255,255,255)',
width = 2
) ),
colorbar = dict(
title = "Millions USD")
) ]
In [133]:
layout2 = dict(
title = '2005 US GDP Distribution<br>(Compared with total awarded amount)',
geo = dict(
scope='usa',
projection=dict( type='albers usa' ),
showlakes = True,
lakecolor = 'rgb(255, 255, 255)'),
)
In [134]:
layout1=dict(
title = 'Administrative Discretionary Grants<br>(Click legend to toggle traces)',
showlegend = True,
geo = dict(
scope='usa',
projection=dict( type='albers usa' ),
showland = True,
landcolor = 'rgb(217, 217, 217)',
subunitwidth=1,
countrywidth=1,
subunitcolor="rgb(255, 255, 255)",
countrycolor="rgb(255, 255, 255)"
),
)
In [131]:
type(trace2)
Out[131]:
In [138]:
from bokeh.io import output_notebook, show
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
output_notebook()
In [237]:
import numpy as np
import matplotlib.pyplot as plt
import csv
from bokeh.io import output_file, show
from bokeh.layouts import widgetbox
from bokeh.models.widgets import Select
In [173]:
x = ["AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI",
"MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA",
"WA","WV","WI","WY"]
y0 = [156750,40063,227358,90319,1766693,220454,210170,53247,83586,700267,388342,58573,47176,586442,245197,123353,104884,143875,199683,46277,263809,345059,395166,243933,81678,224091,30637,73455,118785,57282,443148,74403,1023433,357168,24622,467392,125243,147535,506505,45663,143585,31380,227505,990054,94475,23539,359273,296403,53453,226325,27454]
y1 = [5743215,16189749,19037687,1965330,74663752,22235728,16501999,3857523,25424106,26600593,11216327,14427899,1961349,68112505,16377153,8493716,5764416,8022942,7978539,11177079,21671260,45976945,28799226,15325162,2787779,14911587,10424194,6600356,3677624,4797223,11475362,640574,105860170,29992038,3071608,22015009,12405823,9556529,59575053,4751919,9063478,3596551,18083590,48252344,6884351,6381133,18068081,23476750,927680,17075827,2210361]
In [217]:
x = list(range(1, 52))
y0 = [156750,40063,227358,90319,1766693,220454,210170,53247,83586,700267,388342,58573,47176,586442,245197,123353,104884,143875,199683,46277,263809,345059,395166,243933,81678,224091,30637,73455,118785,57282,443148,74403,1023433,357168,24622,467392,125243,147535,506505,45663,143585,31380,227505,990054,94475,23539,359273,296403,53453,226325,27454]
y1 = [5743215,16189749,19037687,1965330,74663752,22235728,16501999,3857523,25424106,26600593,11216327,14427899,1961349,68112505,16377153,8493716,5764416,8022942,7978539,11177079,21671260,45976945,28799226,15325162,2787779,14911587,10424194,6600356,3677624,4797223,11475362,640574,105860170,29992038,3071608,22015009,12405823,9556529,59575053,4751919,9063478,3596551,18083590,48252344,6884351,6381133,18068081,23476750,927680,17075827,2210361]
In [163]:
type(y0)
Out[163]:
In [176]:
len(x)
Out[176]:
In [232]:
x_label=["IA","IC","IG","IL","IM","IS","LE","LG","LI","LL","LT","MA",
"MG","MH","ML","MN","MP","NC","ND","NE","NG","NL","NO","NP","NR","RE","SP","ST"]
In [200]:
source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1))
In [219]:
TOOLS="pan,wheel_zoom,box_zoom,reset,box_select,lasso_select"
In [233]:
left=plt.bar(x,y0,align="center",width=0.8,alpha=0.5)
plt.xticks(x,x_label)
Out[233]:
In [224]:
left = figure(tools=TOOLS, width=500, height=500, title="GDP/Million",x_axis_label='number represent states,sequence:1~51 means AL~WY')
left.circle('x', 'y0', source=source)
Out[224]:
In [225]:
right = figure(tools=TOOLS, width=500, height=500, title="Total Amount Awarded and Disbursed",x_axis_label='number represent states,sequence:1~51 means AL~WY')
right.circle('x', 'y1', source=source)
Out[225]:
In [234]:
right=plt.bar(x,y1,align="center",width=0.8,alpha=0.5)
plt.xticks(x,x_label)
Out[234]:
In [ ]:
p=plt([[left, right]])
In [240]:
select = Select(title="year:", value="foo", options=["2000", "2001", "2002", "2003","2004","2005"])
show(widgetbox(select))
show(p)
In [ ]: