# Choropleth Maps Exercise - Solutions

Welcome to the Choropleth Maps Exercise! In this exercise we will give you some simple datasets and ask you to create Choropleth Maps from them. Due to the Nature of Plotly we can't show you examples embedded inside the notebook.

Full Documentation Reference

## Plotly Imports

``````

In [13]:

import plotly.graph_objs as go
from plotly.offline import init_notebook_mode,iplot,plot
init_notebook_mode(connected=True)

``````
``````

requirejs.config({paths: { 'plotly': ['https://cdn.plot.ly/plotly-latest.min']},});if(!window.Plotly) {{require(['plotly'],function(plotly) {window.Plotly=plotly;});}}

``````

Import pandas and read the csv file: 2014_World_Power_Consumption

``````

In [2]:

import pandas as pd

``````
``````

In [3]:

``````

Check the head of the DataFrame.

``````

In [4]:

``````
``````

Out[4]:

Country
Power Consumption KWH
Text

0
China
5.523000e+12
China 5,523,000,000,000

1
United States
3.832000e+12
United 3,832,000,000,000

2
European
2.771000e+12
European 2,771,000,000,000

3
Russia
1.065000e+12
Russia 1,065,000,000,000

4
Japan
9.210000e+11
Japan 921,000,000,000

``````

Referencing the lecture notes, create a Choropleth Plot of the Power Consumption for Countries using the data and layout dictionary.

``````

In [19]:

data = dict(
type = 'choropleth',
colorscale = 'Viridis',
reversescale = True,
locations = df['Country'],
locationmode = "country names",
z = df['Power Consumption KWH'],
text = df['Country'],
colorbar = {'title' : 'Power Consumption KWH'},
)

layout = dict(title = '2014 Power Consumption KWH',
geo = dict(showframe = False,projection = {'type':'Mercator'})
)

``````
``````

In [20]:

choromap = go.Figure(data = [data],layout = layout)
plot(choromap,validate=False)

``````
``````

Out[20]:

'file:///Users/marci/Pierian-Data-Courses/Udemy-Python-Data-Science-Machine-Learning/Python-Data-Science-and-Machine-Learning-Bootcamp/Python-for-Data-Visualization/Geographical Plotting/temp-plot.html'

``````

## USA Choropleth

Import the 2012_Election_Data csv file using pandas.

``````

In [7]:

``````

Check the head of the DataFrame.

``````

In [8]:

``````
``````

Out[8]:

Year
ICPSR State Code
Alphanumeric State Code
State
VEP Total Ballots Counted
VEP Highest Office
VAP Highest Office
Total Ballots Counted
Highest Office
Voting-Eligible Population (VEP)
Voting-Age Population (VAP)
% Non-citizen
Prison
Probation
Parole
Total Ineligible Felon
State Abv

0
2012
41
1
Alabama
NaN
58.6%
56.0%
NaN
2,074,338
3,539,217
3707440.0
2.6%
32,232
57,993
8,616
71,584
AL

1
2012
81
2
58.9%
58.7%
55.3%
301,694
300,495
511,792
543763.0
3.8%
5,633
7,173
1,882
11,317
AK

2
2012
61
3
Arizona
53.0%
52.6%
46.5%
2,323,579
2,306,559
4,387,900
4959270.0
9.9%
35,188
72,452
7,460
81,048
AZ

3
2012
42
4
Arkansas
51.1%
50.7%
47.7%
1,078,548
1,069,468
2,109,847
2242740.0
3.5%
14,471
30,122
23,372
53,808
AR

4
2012
71
5
California
55.7%
55.1%
45.1%
13,202,158
13,038,547
23,681,837
28913129.0
17.4%
119,455
0
89,287
208,742
CA

``````

Now create a plot that displays the Voting-Age Population (VAP) per state. If you later want to play around with other columns, make sure you consider their data type. VAP has already been transformed to a float for you.

``````

In [16]:

data = dict(type='choropleth',
colorscale = 'Viridis',
reversescale = True,
locations = usdf['State Abv'],
z = usdf['Voting-Age Population (VAP)'],
locationmode = 'USA-states',
text = usdf['State'],
marker = dict(line = dict(color = 'rgb(255,255,255)',width = 1)),
colorbar = {'title':"Voting-Age Population (VAP)"}
)

``````
``````

In [17]:

layout = dict(title = '2012 General Election Voting Data',
geo = dict(scope='usa',
showlakes = True,
lakecolor = 'rgb(85,173,240)')
)

``````
``````

In [18]:

choromap = go.Figure(data = [data],layout = layout)
plot(choromap,validate=False)

``````
``````

Out[18]:

'file:///Users/marci/Pierian-Data-Courses/Udemy-Python-Data-Science-Machine-Learning/Python-Data-Science-and-Machine-Learning-Bootcamp/Python-for-Data-Visualization/Geographical Plotting/temp-plot.html'

``````