In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

In [2]:
import plotly
import plotly.plotly as py
from plotly.graph_objs import *
import pandas as pd

In [3]:
plotly.tools.set_credentials_file(username='jxljiang221', api_key='LOONcFbgZszSxdiuB3Kn')

In [4]:
df2 = pd.read_csv('C:\\Users\\xiaoliang\\Desktop\\DiscGrants96to13-2017_04_10_19_27_08.csv',encoding='iso-8859-1',sep='\t')
df2.head()


Out[4]:
LogNumber Institution Program ProgramType ProjectTitle ProjectType AwardDate InstAddr1 InstAddr2 InstAddr3 ... FIPSState FIPSCounty CensusTract CensusBlock FIPSMCDCode FIPSPlaceCode CBSACode MetroDivisionCode Description Level
0 IA-00-00-0001-00 Museum of the Aleutians Conservation Assessment Prog. IA NaN NaN 3/30/2000 P.O. Box 648 NaN NaN ... 2.0 16.0 200.0 2014.0 1615.0 80770.0 NaN NaN NaN 1.0
1 IA-00-00-0002-00 Depot Museum, Inc. Conservation Assessment Prog. IA NaN NaN 3/30/2000 P.O. Box 681420 NaN NaN ... 1.0 49.0 961000.0 2013.0 91206.0 27616.0 NaN NaN NaN 1.0
2 IA-00-00-0003-00 National Voting Rights Museum and Institute Conservation Assessment Prog. IA NaN NaN 3/30/2000 6 Highway 80 East NaN NaN ... 1.0 47.0 957200.0 1005.0 92883.0 NaN 42820.0 NaN NaN 1.0
3 IA-00-00-0004-00 Bob Jones Museum Conservation Assessment Prog. IA NaN NaN 3/30/2000 P.O. Box 613 NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN 1.0
4 IA-00-00-0005-00 Coronado Museum of History and Art Conservation Assessment Prog. IA NaN NaN 3/30/2000 1100 Orange Avenue NaN NaN ... 6.0 73.0 10900.0 1021.0 92780.0 16378.0 41740.0 NaN NaN 1.0

5 rows × 44 columns


In [8]:
df2.columns


Out[8]:
Index(['LogNumber', 'Institution', 'Program', 'ProgramType', 'ProjectTitle',
       'ProjectType', 'AwardDate', 'InstAddr1', 'InstAddr2', 'InstAddr3',
       'InstCity', 'InstState', 'InstZip', 'AwardOrig', 'AwardTotal',
       'AppMatchAmount', 'ReqAwardAmount', 'ReqMatchAmount', 'Prefix',
       'FirstName', 'LastName', 'OrgType', 'FiscalYear', 'AwardPeriodFr',
       'AwardPeriodTo', 'OrgUnit', 'OUAddr1', 'OUAddr2', 'OUAddr3', 'OUCity',
       'OUState', 'OUZip', 'Latitude', 'Longitude', 'FIPSState', 'FIPSCounty',
       'CensusTract', 'CensusBlock', 'FIPSMCDCode', 'FIPSPlaceCode',
       'CBSACode', 'MetroDivisionCode', 'Description', 'Level', 'Text'],
      dtype='object')

In [7]:
df2.describe()


C:\Users\xiaoliang\Anaconda3\lib\site-packages\numpy\lib\function_base.py:3834: RuntimeWarning:

Invalid value encountered in percentile

Out[7]:
AwardOrig AwardTotal AppMatchAmount ReqAwardAmount ReqMatchAmount FiscalYear AwardPeriodFr AwardPeriodTo Latitude Longitude FIPSState FIPSCounty CensusTract CensusBlock FIPSMCDCode FIPSPlaceCode CBSACode MetroDivisionCode Level
count 1.319700e+04 1.319700e+04 9.999000e+03 1.319500e+04 1.269900e+04 13197.000000 0.0 0.0 13197.000000 13197.000000 13154.000000 13154.000000 13154.000000 13154.000000 13144.000000 10622.000000 10968.000000 2495.000000 13195.000000
mean 7.116925e+04 7.204923e+04 7.621731e+04 7.448912e+04 6.392099e+04 2003.743578 NaN NaN 40.827064 -97.598385 26.854037 83.128098 273387.381709 2071.108332 67376.552800 41825.046884 30666.816193 31503.599198 1.830315
std 1.612863e+05 1.643848e+05 1.821150e+05 1.934951e+05 1.756425e+05 4.882697 NaN NaN 7.435171 24.147652 16.611920 110.544155 381905.821036 1133.851719 30502.716048 23656.392990 11546.277012 11220.950126 1.035927
min 0.000000e+00 7.150000e+02 0.000000e+00 0.000000e+00 0.000000e+00 1996.000000 NaN NaN -14.276928 -171.694808 1.000000 1.000000 100.000000 47.000000 100.000000 100.000000 10100.000000 11244.000000 1.000000
25% NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
50% NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
75% NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
max 1.000000e+07 1.000000e+07 3.575251e+06 1.125000e+07 3.575251e+06 2013.000000 NaN NaN 70.252870 171.373823 78.000000 840.000000 985600.000000 8002.000000 98000.000000 88900.000000 49780.000000 48864.000000 6.000000

In [5]:
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]) )
    institutions.append(institution)

In [6]:
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[6]:

In [20]:
df2['Text'] = df2['Institution'] + '<br>' + df2['Program'] + '<br>' + 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])]
    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]) )
    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[20]:

In [52]:
df2['Text'] = 'Institution: ' + df2['Institution'] + '<br>' + 'Program: ' + df2['Program'] + '<br>' + 'Prgram 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,10000),(10000,100000),(100000,200000),(200000,500000),(500000,1000000),(1000000,3000000)]
#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'])<limits[i][1])&((df2['AwardTotal'])>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' )

    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' )


---------------------------------------------------------------------------
PlotlyRequestError                        Traceback (most recent call last)
<ipython-input-52-889918a77fd7> in <module>()
     41 
     42 fig = dict( data=institutions, layout=layout )
---> 43 py.iplot( fig, validate=False, filename='q2testworldmap' )

C:\Users\xiaoliang\Anaconda3\lib\site-packages\plotly\plotly\plotly.py in iplot(figure_or_data, **plot_options)
    132     if 'auto_open' not in plot_options:
    133         plot_options['auto_open'] = False
--> 134     url = plot(figure_or_data, **plot_options)
    135 
    136     if isinstance(figure_or_data, dict):

C:\Users\xiaoliang\Anaconda3\lib\site-packages\plotly\plotly\plotly.py in plot(figure_or_data, validate, **plot_options)
    225     data = fig.get('data', [])
    226     plot_options['layout'] = fig.get('layout', {})
--> 227     response = v1.clientresp(data, **plot_options)
    228 
    229     # Check if the url needs a secret key

C:\Users\xiaoliang\Anaconda3\lib\site-packages\plotly\api\v1\clientresp.py in clientresp(data, **kwargs)
     33 
     34     url = '{plotly_domain}/clientresp'.format(**cfg)
---> 35     response = request('post', url, data=payload)
     36 
     37     # Old functionality, just keeping it around.

C:\Users\xiaoliang\Anaconda3\lib\site-packages\plotly\api\v1\utils.py in request(method, url, **kwargs)
     84         content = response.content if response else 'No content'
     85         raise exceptions.PlotlyRequestError(message, status_code, content)
---> 86     validate_response(response)
     87     return response

C:\Users\xiaoliang\Anaconda3\lib\site-packages\plotly\api\v1\utils.py in validate_response(response)
     36         message = content if content else 'No Content'
     37 
---> 38     raise exceptions.PlotlyRequestError(message, status_code, content)
     39 
     40 

PlotlyRequestError: Hey there! You've hit one of our API request limits. 

To get unlimited API calls(10,000/day), please upgrade to a paid plan. 

UPGRADE HERE: https://goo.gl/i7glmM 

Thanks for using Plotly! Happy Plotting!

In [43]:
institutions[4]['text']


Out[43]:
794      Institution: Heritage Preservation, Inc.<br>Pr...
2746     Institution: Missoula Art Museum<br>Program: G...
3143     Institution: American Association of Museums<b...
3144     Institution: American Association of Museums<b...
5039     Institution: StoryCorps, Inc.<br>Program: Nati...
5045     Institution: Urban Libraries Council<br>Progra...
5059     Institution: University of Illinois, Board of ...
5061     Institution: Heritage Preservation, Inc.<br>Pr...
5954     Institution: University of Idaho, Library<br>P...
5987     Institution: University of Denver<br>Program: ...
8065     Institution: ALU LIKE, Inc.<br>Program: Native...
8066     Institution: ALU LIKE, Inc.<br>Program: Native...
9546     Institution: University of Rhode Island, Gradu...
9551     Institution: Board of Trustees, University of ...
9554     Institution: Alaska Native Heritage Center<br>...
9600     Institution: Colorado Seminary<br>Program: Lib...
9848     Institution: Arizona State Library, Archives a...
9970     Institution: University of Illinois, Urbana-Ch...
9976     Institution: Mansfield University of Pennsylva...
9987     Institution: University of Texas at Austin, Of...
10060    Institution: Museum of New Mexico Foundation<b...
10066    Institution: Syracuse University<br>Program: L...
10118    Institution: University of Oklahoma, School of...
10149    Institution: Massachusetts Institute of Techno...
10159    Institution: Board of Trustees, University of ...
10161    Institution: Shelburne Museum<br>Program: Save...
10166    Institution: ALU LIKE, Inc.<br>Program: Native...
10187    Institution: Maine Historical Society<br>Progr...
10195    Institution: University of North Texas, School...
10200    Institution: New York State Education Departme...
                               ...                        
12802    Institution: University of Tennessee<br>Progra...
12811    Institution: Pacific Resources for Education a...
12813    Institution: University of Georgia<br>Program:...
12828    Institution: University of North Carolina at C...
12849    Institution: Pennsylvania Heritage Society<br>...
12853    Institution: University of Tennessee<br>Progra...
12855    Institution: University of Maryland<br>Program...
12881    Institution: Louisiana State University, A&M<b...
12882    Institution: Wildlife Conservation Society<br>...
12884    Institution: American Library Association<br>P...
12886    Institution: Hartford Public Library<br>Progra...
12888    Institution: Indianapolis Museum of Art<br>Pro...
12922    Institution: Stephen F. Austin State Universit...
12940    Institution: Educational Broadcasting Corporat...
12947    Institution: Peggy Notebaert Nature Museum<br>...
12954    Institution: King County Library System<br>Pro...
12963    Institution: University of Wisconsin System<br...
12978    Institution: Regents of the University of Mich...
12979    Institution: Japanese American National Museum...
12985    Institution: University of Maryland<br>Program...
13018    Institution: Rochester Institute of Technology...
13030    Institution: Portland State University, Millar...
13077    Institution: Carnegie Library of Pittsburgh<br...
13146    Institution: Regents of the University of Mich...
13169    Institution: Trustees of Indiana University<br...
13172    Institution: President and Fellows of Harvard ...
13189    Institution: University of California, Los Ang...
13191    Institution: Indiana University<br>Program: Li...
13192    Institution: University of North Texas<br>Prog...
13197    Institution: Regents of the University of Cali...
Name: Text, dtype: object

In [ ]: