How to create visually stunning Choropleth Maps in jupyter Notebooks quickly using plotly.
Importing the required packages. For plotly to work in offline Jupyter notebook we need to import iplot from plotly.offline and initiate notebook mode.

import pandas as pd
import numpy as np  

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

Let’s load some data we can work with. The data contains the Electoral Votes share of each State in the U.S.

#Load the Data
electoral_votes = pd.read_csv('hw2data/electoral_votes.csv')
electoral_votes.head()
State Votes
0 California 55
1 Texas 38
2 New York 29
3 Florida 29
4 Illinois 20

Plotly takes locations in the form of Two-Letter State Codes, so this will come in handy if the data doesn’t come with the State codes.

states_abbrev = {

'AK': 'Alaska', 'AL': 'Alabama', 'AR': 'Arkansas', 'AS': 'American Samoa', 'AZ': 'Arizona', 'CA': 'California', 'CO': 'Colorado', 
'CT':  'Connecticut', 'DC': 'District of Columbia', 'DE': 'Delaware', 'FL': 'Florida', 'GA': 'Georgia', 'GU': 'Guam', 'HI': 'Hawaii',    'IA': 'Iowa', 'ID': 'Idaho', 'IL': 'Illinois', 'IN': 'Indiana', 'KS': 'Kansas', 'KY': 'Kentucky', 'LA': 'Louisiana', 
'MA': 'Massachusetts', 'MD': 'Maryland', 'ME': 'Maine', 'MI': 'Michigan', 'MN': 'Minnesota', 'MO': 'Missouri',
'MP': 'Northern Mariana Islands', 'MS': 'Mississippi', 'MT': 'Montana', 'NA': 'National', 'NC': 'North Carolina', 'ND': 'North Dakota', 'NE': 'Nebraska', 'NH': 'New Hampshire', 'NJ': 'New Jersey', 'NM': 'New Mexico', 'NV': 'Nevada', 'NY': 'New York', 'OH': 'Ohio',
'OK': 'Oklahoma', 'OR': 'Oregon', 'PA': 'Pennsylvania', 'PR': 'Puerto Rico', 'RI': 'Rhode Island', 'SC': 'South Carolina', 
'SD': 'South Dakota', 'TN': 'Tennessee', 'TX': 'Texas', 'UT': 'Utah', 'VA': 'Virginia', 'VI': 'Virgin Islands', 'VT': 'Vermont', 
'WA': 'Washington', 'WI': 'Wisconsin', 'WV': 'West Virginia', 'WY': 'Wyoming'

}

states_abbrev_reverse = dict(zip(states_abbrev.values(),states_abbrev.keys()))

The dataframe does not have the state code, so let’s fix that first.

electoral_votes["Code"] = electoral_votes["State"].apply(lambda x: states_abbrev_reverse[x])
electoral_votes.head()
State Votes Code
0 California 55 CA
1 Texas 38 TX
2 New York 29 NY
3 Florida 29 FL
4 Illinois 20 IL