Choropleth Maps

In [1]:
import plotly.plotly as py
In [2]:
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
In [3]:
init_notebook_mode(connected=True)
In [4]:
# USA Data 
data = dict(type = 'choropleth', 
            locations =['AZ', 'CA', 'NY'],
           locationmode = 'USA-states',
           colorscale ='Greens',
           text = ['Arizona', 'Cali', 'New York'],
           z = [1.0, 2.0, 3.0],
           colorbar = {'title':'Colorbar Title Goes Here'})
In [5]:
data
Out[5]:
{'colorbar': {'title': 'Colorbar Title Goes Here'},
 'colorscale': 'Greens',
 'locationmode': 'USA-states',
 'locations': ['AZ', 'CA', 'NY'],
 'text': ['Arizona', 'Cali', 'New York'],
 'type': 'choropleth',
 'z': [1.0, 2.0, 3.0]}
In [6]:
# layout object
layout = dict(geo = {'scope': 'usa'})
In [7]:
import plotly.graph_objs as go
In [8]:
choromap = go.Figure(data = [data], layout=layout)
In [9]:
iplot(choromap)
In [10]:
import pandas as pd
df = pd.read_csv('2011_US_AGRI_Exports')
df
Out[10]:
code state category total exports beef pork poultry dairy fruits fresh fruits proc total fruits veggies fresh veggies proc total veggies corn wheat cotton text
0 AL Alabama state 1390.63 34.4 10.6 481.0 4.06 8.0 17.1 25.11 5.5 8.9 14.33 34.9 70.0 317.61 Alabama<br>Beef 34.4 Dairy 4.06<br>Fruits 25.1...
1 AK Alaska state 13.31 0.2 0.1 0.0 0.19 0.0 0.0 0.00 0.6 1.0 1.56 0.0 0.0 0.00 Alaska<br>Beef 0.2 Dairy 0.19<br>Fruits 0.0 Ve...
2 AZ Arizona state 1463.17 71.3 17.9 0.0 105.48 19.3 41.0 60.27 147.5 239.4 386.91 7.3 48.7 423.95 Arizona<br>Beef 71.3 Dairy 105.48<br>Fruits 60...
3 AR Arkansas state 3586.02 53.2 29.4 562.9 3.53 2.2 4.7 6.88 4.4 7.1 11.45 69.5 114.5 665.44 Arkansas<br>Beef 53.2 Dairy 3.53<br>Fruits 6.8...
4 CA California state 16472.88 228.7 11.1 225.4 929.95 2791.8 5944.6 8736.40 803.2 1303.5 2106.79 34.6 249.3 1064.95 California<br>Beef 228.7 Dairy 929.95<br>Frui...
5 CO Colorado state 1851.33 261.4 66.0 14.0 71.94 5.7 12.2 17.99 45.1 73.2 118.27 183.2 400.5 0.00 Colorado<br>Beef 261.4 Dairy 71.94<br>Fruits 1...
6 CT Connecticut state 259.62 1.1 0.1 6.9 9.49 4.2 8.9 13.10 4.3 6.9 11.16 0.0 0.0 0.00 Connecticut<br>Beef 1.1 Dairy 9.49<br>Fruits 1...
7 DE Delaware state 282.19 0.4 0.6 114.7 2.30 0.5 1.0 1.53 7.6 12.4 20.03 26.9 22.9 0.00 Delaware<br>Beef 0.4 Dairy 2.3<br>Fruits 1.53 ...
8 FL Florida state 3764.09 42.6 0.9 56.9 66.31 438.2 933.1 1371.36 171.9 279.0 450.86 3.5 1.8 78.24 Florida<br>Beef 42.6 Dairy 66.31<br>Fruits 137...
9 GA Georgia state 2860.84 31.0 18.9 630.4 38.38 74.6 158.9 233.51 59.0 95.8 154.77 57.8 65.4 1154.07 Georgia<br>Beef 31.0 Dairy 38.38<br>Fruits 233...
10 HI Hawaii state 401.84 4.0 0.7 1.3 1.16 17.7 37.8 55.51 9.5 15.4 24.83 0.0 0.0 0.00 Hawaii<br>Beef 4.0 Dairy 1.16<br>Fruits 55.51 ...
11 ID Idaho state 2078.89 119.8 0.0 2.4 294.60 6.9 14.7 21.64 121.7 197.5 319.19 24.0 568.2 0.00 Idaho<br>Beef 119.8 Dairy 294.6<br>Fruits 21.6...
12 IL Illinois state 8709.48 53.7 394.0 14.0 45.82 4.0 8.5 12.53 15.2 24.7 39.95 2228.5 223.8 0.00 Illinois<br>Beef 53.7 Dairy 45.82<br>Fruits 12...
13 IN Indiana state 5050.23 21.9 341.9 165.6 89.70 4.1 8.8 12.98 14.4 23.4 37.89 1123.2 114.0 0.00 Indiana<br>Beef 21.9 Dairy 89.7<br>Fruits 12.9...
14 IA Iowa state 11273.76 289.8 1895.6 155.6 107.00 1.0 2.2 3.24 2.7 4.4 7.10 2529.8 3.1 0.00 Iowa<br>Beef 289.8 Dairy 107.0<br>Fruits 3.24 ...
15 KS Kansas state 4589.01 659.3 179.4 6.4 65.45 1.0 2.1 3.11 3.6 5.8 9.32 457.3 1426.5 43.98 Kansas<br>Beef 659.3 Dairy 65.45<br>Fruits 3.1...
16 KY Kentucky state 1889.15 54.8 34.2 151.3 28.27 2.1 4.5 6.60 0.0 0.0 0.00 179.1 149.3 0.00 Kentucky<br>Beef 54.8 Dairy 28.27<br>Fruits 6....
17 LA Louisiana state 1914.23 19.8 0.8 77.2 6.02 5.7 12.1 17.83 6.6 10.7 17.25 91.4 78.7 280.42 Louisiana<br>Beef 19.8 Dairy 6.02<br>Fruits 17...
18 ME Maine state 278.37 1.4 0.5 10.4 16.18 16.6 35.4 52.01 24.0 38.9 62.90 0.0 0.0 0.00 Maine<br>Beef 1.4 Dairy 16.18<br>Fruits 52.01 ...
19 MD Maryland state 692.75 5.6 3.1 127.0 24.81 4.1 8.8 12.90 7.8 12.6 20.43 54.1 55.8 0.00 Maryland<br>Beef 5.6 Dairy 24.81<br>Fruits 12....
20 MA Massachusetts state 248.65 0.6 0.5 0.6 5.81 25.8 55.0 80.83 8.1 13.1 21.13 0.0 0.0 0.00 Massachusetts<br>Beef 0.6 Dairy 5.81<br>Fruits...
21 MI Michigan state 3164.16 37.7 118.1 32.6 214.82 82.3 175.3 257.69 72.4 117.5 189.96 381.5 247.0 0.00 Michigan<br>Beef 37.7 Dairy 214.82<br>Fruits 2...
22 MN Minnesota state 7192.33 112.3 740.4 189.2 218.05 2.5 5.4 7.91 45.9 74.5 120.37 1264.3 538.1 0.00 Minnesota<br>Beef 112.3 Dairy 218.05<br>Fruits...
23 MS Mississippi state 2170.80 12.8 30.4 370.8 5.45 5.4 11.6 17.04 10.6 17.2 27.87 110.0 102.2 494.75 Mississippi<br>Beef 12.8 Dairy 5.45<br>Fruits ...
24 MO Missouri state 3933.42 137.2 277.3 196.1 34.26 4.2 9.0 13.18 6.8 11.1 17.90 428.8 161.7 345.29 Missouri<br>Beef 137.2 Dairy 34.26<br>Fruits 1...
25 MT Montana state 1718.00 105.0 16.7 1.7 6.82 1.1 2.2 3.30 17.3 28.0 45.27 5.4 1198.1 0.00 Montana<br>Beef 105.0 Dairy 6.82<br>Fruits 3.3...
26 NE Nebraska state 7114.13 762.2 262.5 31.4 30.07 0.7 1.5 2.16 20.4 33.1 53.50 1735.9 292.3 0.00 Nebraska<br>Beef 762.2 Dairy 30.07<br>Fruits 2...
27 NV Nevada state 139.89 21.8 0.2 0.0 16.57 0.4 0.8 1.19 10.6 17.3 27.93 0.0 5.4 0.00 Nevada<br>Beef 21.8 Dairy 16.57<br>Fruits 1.19...
28 NH New Hampshire state 73.06 0.6 0.2 0.8 7.46 2.6 5.4 7.98 1.7 2.8 4.50 0.0 0.0 0.00 New Hampshire<br>Beef 0.6 Dairy 7.46<br>Fruits...
29 NJ New Jersey state 500.40 0.8 0.4 4.6 3.37 35.0 74.5 109.45 21.6 35.0 56.54 10.1 6.7 0.00 New Jersey<br>Beef 0.8 Dairy 3.37<br>Fruits 10...
30 NM New Mexico state 751.58 117.2 0.1 0.3 191.01 32.6 69.3 101.90 16.7 27.1 43.88 11.2 13.9 72.62 New Mexico<br>Beef 117.2 Dairy 191.01<br>Fruit...
31 NY New York state 1488.90 22.2 5.8 17.7 331.80 64.7 137.8 202.56 54.7 88.7 143.37 106.1 29.9 0.00 New York<br>Beef 22.2 Dairy 331.8<br>Fruits 20...
32 NC North Carolina state 3806.05 24.8 702.8 598.4 24.90 23.8 50.7 74.47 57.4 93.1 150.45 92.2 200.3 470.86 North Carolina<br>Beef 24.8 Dairy 24.9<br>Frui...
33 ND North Dakota state 3761.96 78.5 16.1 0.5 8.14 0.1 0.2 0.25 49.9 80.9 130.79 236.1 1664.5 0.00 North Dakota<br>Beef 78.5 Dairy 8.14<br>Fruits...
34 OH Ohio state 3979.79 36.2 199.1 129.9 134.57 8.7 18.5 27.21 20.4 33.1 53.53 535.1 207.4 0.00 Ohio<br>Beef 36.2 Dairy 134.57<br>Fruits 27.21...
35 OK Oklahoma state 1646.41 337.6 265.3 131.1 24.35 3.0 6.3 9.24 3.4 5.5 8.90 27.5 324.8 110.54 Oklahoma<br>Beef 337.6 Dairy 24.35<br>Fruits 9...
36 OR Oregon state 1794.57 58.8 1.4 14.2 63.66 100.7 214.4 315.04 48.2 78.3 126.50 11.7 320.3 0.00 Oregon<br>Beef 58.8 Dairy 63.66<br>Fruits 315....
37 PA Pennsylvania state 1969.87 50.9 91.3 169.8 280.87 28.6 60.9 89.48 14.6 23.7 38.26 112.1 41.0 0.00 Pennsylvania<br>Beef 50.9 Dairy 280.87<br>Frui...
38 RI Rhode Island state 31.59 0.1 0.1 0.2 0.52 0.9 1.9 2.83 1.2 1.9 3.02 0.0 0.0 0.00 Rhode Island<br>Beef 0.1 Dairy 0.52<br>Fruits ...
39 SC South Carolina state 929.93 15.2 10.9 186.5 7.62 17.1 36.4 53.45 16.3 26.4 42.66 32.1 55.3 206.10 South Carolina<br>Beef 15.2 Dairy 7.62<br>Frui...
40 SD South Dakota state 3770.19 193.5 160.2 29.3 46.77 0.3 0.5 0.80 1.5 2.5 4.06 643.6 704.5 0.00 South Dakota<br>Beef 193.5 Dairy 46.77<br>Frui...
41 TN Tennessee state 1535.13 51.1 17.6 82.4 21.18 2.0 4.2 6.23 9.4 15.3 24.67 88.8 100.0 363.83 Tennessee<br>Beef 51.1 Dairy 21.18<br>Fruits 6...
42 TX Texas state 6648.22 961.0 42.7 339.2 240.55 31.9 68.0 99.90 43.9 71.3 115.23 167.2 309.7 2308.76 Texas<br>Beef 961.0 Dairy 240.55<br>Fruits 99....
43 UT Utah state 453.39 27.9 59.0 23.1 48.60 3.9 8.4 12.34 2.5 4.1 6.60 5.3 42.8 0.00 Utah<br>Beef 27.9 Dairy 48.6<br>Fruits 12.34 V...
44 VT Vermont state 180.14 6.2 0.2 0.9 65.98 2.6 5.4 8.01 1.5 2.5 4.05 0.0 0.0 0.00 Vermont<br>Beef 6.2 Dairy 65.98<br>Fruits 8.01...
45 VA Virginia state 1146.48 39.5 16.9 164.7 47.85 11.7 24.8 36.48 10.4 16.9 27.25 39.5 77.5 64.84 Virginia<br>Beef 39.5 Dairy 47.85<br>Fruits 36...
46 WA Washington state 3894.81 59.2 0.0 35.6 154.18 555.6 1183.0 1738.57 138.7 225.1 363.79 29.5 786.3 0.00 Washington<br>Beef 59.2 Dairy 154.18<br>Fruits...
47 WV West Virginia state 138.89 12.0 0.3 45.4 3.90 3.7 7.9 11.54 0.0 0.0 0.00 3.5 1.6 0.00 West Virginia<br>Beef 12.0 Dairy 3.9<br>Fruits...
48 WI Wisconsin state 3090.23 107.3 38.6 34.5 633.60 42.8 91.0 133.80 56.8 92.2 148.99 460.5 96.7 0.00 Wisconsin<br>Beef 107.3 Dairy 633.6<br>Fruits ...
49 WY Wyoming state 349.69 75.1 33.2 0.1 2.89 0.1 0.1 0.17 3.9 6.3 10.23 9.0 20.7 0.00 Wyoming<br>Beef 75.1 Dairy 2.89<br>Fruits 0.17...
In [11]:
data = dict(type = 'choropleth', 
           colorscale = 'YIOrRd',
           locations = df['code'],
           locationmode ='USA-states',
           z = df['total exports'], 
           text = df['text'],
           marker = dict(line= dict(color = 'rgb(12, 12, 12)', width=4)),
           colorbar = {'title': 'Millions USD'}
           )
In [12]:
layout = dict(title = '2011 US Agriculture Exports by State',
             geo = dict(scope ='usa', showlakes=True, lakecolor = 'rgb(85, 173, 240)'))
In [13]:
layout
Out[13]:
{'geo': {'lakecolor': 'rgb(85, 173, 240)', 'scope': 'usa', 'showlakes': True},
 'title': '2011 US Agriculture Exports by State'}
In [14]:
choromap2 = go.Figure(data=[data], layout=layout)
In [15]:
iplot(choromap2)

California had the highest export in 2011. Well, considering that I did see a lot of fruits and veggies coming from Cali in my local grocery stores.

In [16]:
# World GDP Dataset 
df = pd.read_csv('2014_World_GDP')
df.head()
Out[16]:
COUNTRY GDP (BILLIONS) CODE
0 Afghanistan 21.71 AFG
1 Albania 13.40 ALB
2 Algeria 227.80 DZA
3 American Samoa 0.75 ASM
4 Andorra 4.80 AND
In [17]:
data = dict(type = 'choropleth',
           locations = df['CODE'],
           z = df['GDP (BILLIONS)'],
           text = df['COUNTRY'],
           colorbar = {'title': 'GDP in Billions US'})
In [18]:
layout = dict(title = '2014 Global GDP',
             geo = dict (showframe = False, projection = {'type': 'natural earth'}))
In [19]:
choromap3 = go.Figure(data=[data], layout=layout)
In [20]:
iplot(choromap3)

USA had the highest GDP and China followed in the second place in 2014.