In [2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
In [96]:
import plotly
import plotly.plotly as py
from plotly.graph_objs import *
import pandas as pd
In [289]:
plotly.tools.set_credentials_file(username='jxljiang221', api_key='RgXxTYxCam4vBca8DHDq')
In [9]:
import os
In [11]:
os.getcwd()
Out[11]:
In [274]:
df2 = pd.read_csv('DiscGrants96to13-2017_04_10_19_27_08.csv',encoding='iso-8859-1',sep='\t')
df2.head()
Out[274]:
In [282]:
df3=df2[(df2["Longitude"]>-95)&(df2["Latitude"]<24)]
#df2=df2[(((df2["Longitude"]>-95)&(df2["Latitude"]<24))==False)]
df2=df2[((df2["InstState"]=="GU")|(df2["InstState"]=="VI")|(df2["InstState"]=="PR")|(df2["InstState"]=="MP")|(df2["InstState"]=="FM")|(df2["InstState"]=="MH")|(df2["InstState"]=="AS"))==False]
#VI,GU,PR,MP,FM,MH
# since some points (Saint Thomas, U.S. Virgin Islands) could not be shown on the US map, so I tried to remove them by using the codes above.
In [283]:
#df2.columns
In [284]:
#df2.describe()
In [285]:
df2['Text'] = df2['Institution'] + '<br>' + df2['Program'] + '<br>' + df2['ProgramType'] + 'Total Award' + (df2['AwardTotal']/1e3).astype(str)+ ' thousand'
#df2['text'] = df2['Institution'] + 'Total Award' + (df2['AwardTotal']/1e3).astype(str)+ ' thousand'
limits = [(0,10),(10,100),(100,200),(200,500),(500,1000),(1000,3000)]
#colors = ["#ffcccc","#ffddcc","#ffeecc","#ffffcc","#eeffcc","#ddffcc"]
colors = ["e0e0e0","#66b2ff","#66ff66","#ffff66","#ffb266","#ff6666"]
institutions = []
scale = 6000
for i in range(len(limits)):
subdf2=df2[((df2['AwardTotal']/1e3)<limits[i][1])&((df2['AwardTotal']/1e3)>limits[i][0])]
institution = dict(
type = 'scattergeo',
locationmode = 'USA-states',
lon = subdf2['Longitude'],
lat = subdf2['Latitude'],
text = subdf2['Text'],
marker = dict(
size = subdf2['AwardTotal']/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])+' thousand dollar' )
institutions.append(institution)
In [286]:
layout = 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)"
),
updatemenus=list([
dict(
x=-0.05,
y=1,
yanchor='top',
buttons=list([
dict(
args=['visible', [True, True, True, True, True, True]],
label='All',
method='restyle'
),
dict(
args=['visible', [True, False, False, False, False, False]],
label='0-10 thousand dollar',
method='restyle'
),
dict(
args=['visible', [False, True, False, False, False, False]],
label='10-100 thousand dollar',
method='restyle'
),
dict(
args=['visible', [False, False, True, False, False, False]],
label='100-200 thousand dollar',
method='restyle'
),
dict(
args=['visible', [False, False, False, True, False, False]],
label='200-500 thousand dollar',
method='restyle'
),
dict(
args=['visible', [False, False, False, False, True, False]],
label='500-1000 thousand dollar',
method='restyle'
),
dict(
args=['visible', [False, False, False, False, False, True]],
label='1000+ thousand dollar',
method='restyle'
)
]),
)
]),
)
fig = dict( data=institutions, layout=layout )
py.iplot( fig, validate=False, filename='q2testworldmap' )
Out[286]:
In [270]:
df2['Text'] = df2['Institution'] + '<br>' + df2['Program'] + '<br>' + df2['ProgramType'] + 'Total Award' + (df2['AwardTotal']/1e3).astype(str)+ ' thousand'
#df2['text'] = df2['Institution'] + 'Total Award' + (df2['AwardTotal']/1e3).astype(str)+ ' thousand'
#limits = [(0,10),(10,100),(100,200),(200,500),(500,1000),(1000,3000)]
#colors = ["#ffcccc","#ffddcc","#ffeecc","#ffffcc","#eeffcc","#ddffcc"]
colors = ["#ff6666","#ffb266","#ffff66","#b2ff66","#66ff66","#66ffb2","#66ffff","#66b2ff","#6666ff","#ff66ff","#b266ff","#ff66b2","#000000","#404040","#808080","#c0c0c0","#ffffff"]
pt=["IL","ST","ML","RE","IC","IM","IS","MN","LE","MP","MH","LT","LI","IG","MA","IA","IG"]
institutions = []
scale = 5000
for i in range(len(colors)):
subdf2=df2[df2["ProgramType"]==pt[16-i]]
institution = dict(
type = 'scattergeo',
locationmode = 'USA-states',
lon = subdf2['Longitude'],
lat = subdf2['Latitude'],
text = subdf2['Text'],
marker = dict(
size = subdf2['AwardTotal']/scale,
color = colors[16-i],
line = dict(width=0.5, color='rgb(40,40,40)'),
sizemode = 'area'
),
name =pt[i])
institutions.append(institution)
layout = 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)"
)
)
fig = dict( data=institutions, layout=layout )
py.iplot( fig, validate=False, filename='q2testworldmap' )
Out[270]:
In [271]:
df2['Text'] = df2['Institution'] + '<br>' + df2['Program'] + '<br>' + df2['ProgramType'] + 'Total Award' + (df2['AwardTotal']/1e3).astype(str)+ ' thousand'
#df2['text'] = df2['Institution'] + 'Total Award' + (df2['AwardTotal']/1e3).astype(str)+ ' thousand'
#limits = [(0,10),(10,100),(100,200),(200,500),(500,1000),(1000,3000)]
#colors = ["#ffcccc","#ffddcc","#ffeecc","#ffffcc","#eeffcc","#ddffcc"]
colors = ["#ff6666","#ffb266","#ffff66","#b2ff66","#66ff66","#66ffb2","#66ffff","#66b2ff","#6666ff","#ff66ff","#b266ff","#ff66b2","#000000","#404040","#808080","#c0c0c0","#ffffff"]
pt=["IL","ST","ML","RE","IC","IM","IS","MN","LE","MP","MH","LT","LI","IG","MA","IA","IG"]
institutions = []
scale = 6000
for i in range(len(colors)):
subdf2=df2[df2["ProgramType"]==pt[i]]
institution = dict(
type = 'scattergeo',
locationmode = 'USA-states',
lon = subdf2['Longitude'],
lat = subdf2['Latitude'],
text = subdf2['Text'],
marker = dict(
size = subdf2['AwardTotal']/scale,
color = colors[16-i],
line = dict(width=0.5, color='rgb(40,40,40)'),
sizemode = 'area'
),
name =pt[i])
institutions.append(institution)
layout = 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)"
)
)
fig = dict( data=institutions, layout=layout )
py.iplot( fig, validate=False, filename='q2testworldmap' )
Out[271]:
In [272]:
df2['Text'] = df2['Institution'] + '<br>' + df2['Program'] + '<br>' + 'Program Type: ' + df2['ProgramType'] + '<br>'+ 'Total Award: ' + (df2['AwardTotal']/1e3).astype(str)+ ' thousand'
#df2['text'] = df2['Institution'] + 'Total Award' + (df2['AwardTotal']/1e3).astype(str)+ ' thousand'
limits = [(0,10),(10,100),(100,200),(200,500),(500,1000),(1000,3000)]
#colors = ["#ffcccc","#ffddcc","#ffeecc","#ffffcc","#eeffcc","#ddffcc"]
colors = ["e0e0e0","#66b2ff","#66ff66","#ffff66","#ffb266","#ff6666"]
institutions = []
scale = 6000
for i in range(len(limits)):
subdf2=df2[((df2['AwardTotal']/1e3)<limits[i][1])&((df2['AwardTotal']/1e3)>limits[i][0])]
subdf2=subdf2[1:50]
institution = dict(
type = 'scattergeo',
locationmode = 'USA-states',
lon = subdf2['Longitude'],
lat = subdf2['Latitude'],
text = subdf2['Text'],
marker = dict(
size = subdf2['AwardTotal']/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])+' thousand dollar' )
institutions.append(institution)
layout = dict(
title = 'Temp Scatter Plot of Top 50 for 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)"
)
)
fig = dict( data=institutions, layout=layout )
py.iplot( fig, validate=False, filename='q2testworldmap' )
Out[272]:
In [290]:
df2['Text'] = df2['Institution'] + '<br>' + df2['Program'] + '<br>' + df2['ProgramType'] + 'Total Award' + (df2['AwardTotal']/1e3).astype(str)+ ' thousand'
#df2['text'] = df2['Institution'] + 'Total Award' + (df2['AwardTotal']/1e3).astype(str)+ ' thousand'
limits = [(0,10),(10,100),(100,200),(200,500),(500,1000),(1000,3000)]
#colors = ["#ffcccc","#ffddcc","#ffeecc","#ffffcc","#eeffcc","#ddffcc"]
colors = ["e0e0e0","#66b2ff","#66ff66","#ffff66","#ffb266","#ff6666"]
institutions = []
scale = 6000
for i in range(len(limits)):
subdf2=df2[((df2['AwardTotal']/1e3)<limits[i][1])&((df2['AwardTotal']/1e3)>limits[i][0])]
institution = dict(
type = 'scattergeo',
locationmode = 'ISO-3',
lon = subdf2['Longitude'],
lat = subdf2['Latitude'],
text = subdf2['Text'],
marker = dict(
size = subdf2['AwardTotal']/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])+' thousand dollar' )
institutions.append(institution)
layout = 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)"
)
)
fig = dict( data=institutions, layout=layout )
py.iplot( fig, validate=False, filename='q2testworldmap' )
Out[290]:
In [ ]: