In [23]:
import plotly 
plotly.tools.set_credentials_file(username='yuxiangling0809', api_key='vJxzgz9EZWkJZdHur9A8')

In [24]:
import plotly 
plotly.tools.set_config_file(world_readable=True,
                             sharing='public')

In [25]:
import plotly.plotly as py

In [26]:
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]:
Log Number Institution Program Program Type Project Title Project Type Award Date Address Address 2 Address 3 ... Census Tract Census Block MCD Code Place Code CBSA Code MSAD Code Description Location Organizational Unit Location text
0 IM-01-02-0010-02 Fire Museum of Maryland Museum Assessment Program IM NaN Institutional 3/27/02 0:00 1301 York Road NaN NaN ... 408703.0 2001.0 90656.0 51587.0 12580.0 NaN NaN 1301 York Road\rLutherville, MD 21093\r(39.417... NaN Museum Assessment Program<br>Total Amount Awar...
1 IM-01-03-0032-03 Loxahatchee River Historical Museum Museum Assessment Program IM NaN Institutional 38020 805 North U.S. Highway 1 NaN NaN ... 408.0 2003.0 91690.0 35875.0 33100.0 48424.0 NaN 805 North U.S. Highway 1\rJupiter, FL 33477\r(... NaN Museum Assessment Program<br>Total Amount Awar...
2 LL-30-99-0194-99 Museum of Science and Industry, Chicago National Leadership Grants (LL) LL NaN NLG LMC 7/12/99 0:00 57th Street and Lake Shore Drive NaN NaN ... 411000.0 1008.0 14000.0 14000.0 16980.0 16974.0 NaN 57th Street and Lake Shore Drive\rChicago, IL ... NaN National Leadership Grants (LL)<br>Total Amoun...
3 IC-22-10-0036-10 County of Delaware Conservation Program IC Collection Storage Improvement AHPG-Library 2/22/10 0:00 44 Burrer Drive NaN NaN ... 11604.0 2023.0 75620.0 75602.0 18140.0 NaN The County of Delaware Community Library in Su... 44 Burrer Drive\rSunbury, OH 43074-9319\r(40.2... NaN Conservation Program<br>Total Amount Awarded a...
4 IC-21-11-0142-11 Plimoth Plantation Conservation Program IC Rare Book Rehousing Project AHPG-Museum 37875 P.O. Box 1620 NaN NaN ... 530600.0 1040.0 54310.0 NaN 14460.0 14454.0 Plimoth Plantation will re-house the museum's ... P.O. Box 1620\rPlymouth, MA 02362-1620\r NaN Conservation Program<br>Total Amount Awarded a...

5 rows × 40 columns


In [82]:
df1.head()


Out[82]:
z locations text Unnamed: 3 Unnamed: 4 Unnamed: 5 Unnamed: 6 Unnamed: 7 GDP
0 156750.0 AL State: AL<br>Total Awarded Amount: 5743215<br>... NaN NaN NaN NaN NaN 156750.0
1 40063.0 AK State: AK<br>Total Awarded Amount: 16189749<br... NaN NaN NaN NaN NaN 40063.0
2 227358.0 AZ State: AZ<br>Total Awarded Amount: 19037687<br... NaN NaN NaN NaN NaN 227358.0
3 90319.0 AR State: AR<br>Total Awarded Amount: 1965330<br>... NaN NaN NaN NaN NaN 90319.0
4 1766693.0 CA State: CA<br>Total Awarded Amount: 74663752<br... NaN NaN NaN NaN NaN 1766693.0

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 [87]:
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 [88]:
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 [89]:
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 [90]:
fig1 = dict( data=trace1, layout=layout1 )
py.iplot(fig1)


Out[90]:

In [ ]:
fig2 = dict( data=trace2, layout=layout2 )
py.iplot(fig2)

In [49]:
from bokeh.io import output_notebook, show
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure

output_notebook()


Loading BokehJS ...

In [50]:
TOOLS = "box_select,lasso_select,help"

In [62]:
source1 = ColumnDataSource(data=df)
source2 = ColumnDataSource(data=df1)

In [63]:
left = figure(tools=TOOLS, width=300, height=300, title=None)
left.circle('Total Amount Awarded and Disbursed','State',source=source1)


Out[63]:
GlyphRenderer(
id = '0792e3bd-6eed-4bb7-9911-90c5ab942ffb', …)

In [77]:
right = figure(tools=TOOLS, width=300, height=300, title=None)
right.circle('z','locations', source=source2)


Out[77]:
GlyphRenderer(
id = '4329d855-10a9-4aea-8fc2-20798c531b7c', …)

In [78]:
p = gridplot([[left, right]])

In [79]:
show(p)



In [ ]: