Choropleth Maps Practice


In [11]:
import plotly.graph_objs as go 
import pandas as pd
from plotly.offline import init_notebook_mode,iplot
init_notebook_mode(connected=True)


Reading the csv file: 2014_World_Power_Consumption


In [12]:
df = pd.read_csv('2014_World_Power_Consumption')

In [13]:
df.head()


Out[13]:
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

Checking the head of the DataFrame.


In [14]:
df.head()


Out[14]:
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

Creating a Choropleth Plot of the Power Consumption for Countries using the data and layout dictionary.


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

In [16]:
layout = dict(title = 'Power Consumption for Countries', geo = dict(showframe = False))

In [17]:
choromap = go.Figure(data = [data],layout = layout)
iplot(choromap,validate=False)


USA Choropleth

Loading 2012_Election_Data csv file using pandas.


In [18]:
df = pd.read_csv('2012_Election_Data')

Head of the DataFrame.


In [19]:
df.head()


Out[19]:
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 Alaska 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

In [20]:
data = dict(type='choropleth',
           colorscale = 'Portland',
           locations = df['State Abv'],
           z = df['Voting-Age Population (VAP)'],
           locationmode = "USA-states",
           text = df['State'],
           colorbar = {'title': 'VAP per state'})

In [21]:
layout = dict(title = 'Voting-Age Population (VAP) per state',
                geo = dict(scope='usa',showframe = False,projection = {'type':'Mercator'}))

In [22]:
choromap = go.Figure(data = [data],layout = layout)
iplot(choromap,validate=False)