Choropleth

Author

Dr Stephy Thomas, Bishop Chulaparambil Memorial College, Kottayam

Choropleth of US Unemployment Data for 2023

import plotly.io as pio
pio.renderers.default = "notebook_connected"

Plot the Unemployment Rate given in the dataset on the map of America.

We use the data set available in kaggle.

import pandas as pd
data=pd.read_csv('/home/stephy/Documents/class/Python/Sem 4/UnemploymentUSA.csv')
data.head(3)
FIPS Code State/Area code Year Month Total Civilian Non-Institutional Population in State/Area Total Civilian Labor Force in State/Area Percent (%) of State/Area's Population Total Employment in State/Area Percent (%) of Labor Force Employed in State/Area Total Unemployment in State/Area Percent of Labor Force Unemployed in State/Area
0 1 Alabama AL 1976 1 2605000 1484555 57.0 1386023 53.2 98532 6.6
1 2 Alaska AK 1976 1 232000 160183 69.0 148820 64.1 11363 7.1
2 4 Arizona AZ 1976 1 1621000 964120 59.5 865871 53.4 98249 10.2
data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 28200 entries, 0 to 28199
Data columns (total 12 columns):
 #   Column                                                     Non-Null Count  Dtype  
---  ------                                                     --------------  -----  
 0   FIPS Code                                                  28200 non-null  int64  
 1   State/Area                                                 28200 non-null  object 
 2   code                                                       28200 non-null  object 
 3   Year                                                       28200 non-null  int64  
 4   Month                                                      28200 non-null  int64  
 5   Total Civilian Non-Institutional Population in State/Area  28200 non-null  int64  
 6   Total Civilian Labor Force in State/Area                   28200 non-null  int64  
 7   Percent (%) of State/Area's Population                     28200 non-null  float64
 8   Total Employment in State/Area                             28200 non-null  int64  
 9   Percent (%) of Labor Force Employed in State/Area          28200 non-null  float64
 10  Total Unemployment in State/Area                           28200 non-null  int64  
 11  Percent of Labor Force Unemployed in State/Area            28200 non-null  float64
dtypes: float64(3), int64(7), object(2)
memory usage: 2.6+ MB
data.columns
Index(['FIPS Code', 'State/Area', 'code', 'Year', 'Month',
       'Total Civilian Non-Institutional Population in State/Area',
       'Total Civilian Labor Force in State/Area',
       'Percent (%) of State/Area's Population',
       'Total Employment in State/Area',
       'Percent (%) of Labor Force Employed in State/Area',
       'Total Unemployment in State/Area',
       'Percent of Labor Force Unemployed in State/Area'],
      dtype='object')
data.Year.dtype
data.Month.dtype
dtype('int64')

We we are going to select only relevant columns from the data viz., ‘State/Area’, ‘Year’, ‘Month’, ‘Percent of Labor Force Unemployed in State/Area’. We store the new data with the name df.

df=data[['State/Area','code', 'Year', 'Month', 'Percent of Labor Force Unemployed in State/Area']]
df.shape
df.columns
Index(['State/Area', 'code', 'Year', 'Month',
       'Percent of Labor Force Unemployed in State/Area'],
      dtype='object')
df.columns
Index(['State/Area', 'code', 'Year', 'Month',
       'Percent of Labor Force Unemployed in State/Area'],
      dtype='object')
df['Percent of Labor Force Unemployed in State/Area'].dtype
dtype('float64')

Now select the unemployment rate for the December 2022.

df_22=df[(df['Year']==2022) & (df['Month']==12)]
df_22.head(3)
State/Area code Year Month Percent of Labor Force Unemployed in State/Area
28150 Alabama AL 2022 12 2.6
28151 Alaska AK 2022 12 3.7
28152 Arizona AZ 2022 12 4.0
import plotly.express as px
import numpy as np
fig = px.choropleth(df_22, locations='code',color=df_22['Percent of Labor Force Unemployed in State/Area'].astype(float),  # Column containing country names for hover information
locationmode='USA-states',labels={'Percent of Labor Force Unemployed in State/Area': 'Unemployment Rate'})
fig.show()

The current choropleth displays data for US states on a world map. To focus on the Americas, we can use the scope='usa' argument in the choropleth function. This will switch the map projection to encompass only the North and South American continents.

fig = px.choropleth(df_22, locations='code',color=df_22['Percent of Labor Force Unemployed in State/Area'].astype(float),  # Column containing country names for hover information
    locationmode='USA-states', scope='usa',labels={'Percent of Labor Force Unemployed in State/Area':'Unemployment'})
fig.show()

Currently, the choropleth utilizes distinct colors to represent unemployment rates across states. However, for a more intuitive representation of unemployment severity, we can employ a continuous color scale. This approach utilizes a single color that gradually intensifies as unemployment rates increase, providing a clearer visual gradient for unemployment magnitude across different states.

fig = px.choropleth(df_22, locations='code',color=df_22['Percent of Labor Force Unemployed in State/Area'].astype(float),  # Column containing country names for hover information
    locationmode='USA-states', scope='usa',color_continuous_scale='Blues',labels={'Percent of Labor Force Unemployed in State/Area':'Unemployment Rate'})
fig.show()

Some commonly used options include:

  • viridis (default): Green to yellow to red

  • plasma: Purple to green to yellow

  • cividis: Blue to green to yellow to red

  • inferno: Black to red to yellow

  • magma: Black to red to white

  • turbo: Blue to violet to red to orange

  • Reds: Red in varying intensity

  • Blues: Red in varying intensity

  • Greens: Red in varying intensity

Now we will assign heading to the colour bar as unemployment rate using the function fig.update_layout(coloraxis_colorbar_title_text = 'Unemployment Rate')

fig = px.choropleth(df_22, locations='code',color=df_22['Percent of Labor Force Unemployed in State/Area'].astype(float),  # Column containing country names for hover information
    locationmode='USA-states', 
    scope='usa',color_continuous_scale='Reds',labels={'Percent of Labor Force Unemployed in State/Area','Unemployment Rate'}
)
fig.update_layout(coloraxis_colorbar_title_text = 'Unemployment Rate')
# Show the map
fig.show()

Currently, the hover text for the choropleth displays basic information like “Code” and “Color”, color representing the unemployment rate. To provide more context and insights, let’s enrich the hover text to include the following details:

  • State Name: This identifies the specific geographic location represented by the data point.

  • Unemployment Rate: This is the core data value visualized using the color gradient, allowing users to easily see the unemployment rate for each state upon hovering.

By incorporating these elements, the hover text becomes more informative and user-friendly, enhancing the user’s understanding of the data they’re interacting with.

import numpy as np
fig = px.choropleth(df_22, locations='code',color=df_22['Percent of Labor Force Unemployed in State/Area'].astype(float),  # Column containing country names for hover information
    locationmode='USA-states', 
    scope='usa',color_continuous_scale='Reds',labels={'Percent of Labor Force Unemployed in State/Area':'Unemployment Rate'}
)
fig.update_layout(coloraxis_colorbar_title_text = 'Unemployment Rate')
fig.update_traces(
    customdata= np.stack((df_22['State/Area'], df_22['Percent of Labor Force Unemployed in State/Area']), axis=-1),
    hovertemplate=(
        "<b>%{customdata[0]}</b><br>"+\
        "Unemployment Rate: %{customdata[1]}"
    )
)
# Show the map
fig.show()

Lets customize the the borders separating the states.

import numpy as np
fig = px.choropleth(df_22, locations='code',color=df_22['Percent of Labor Force Unemployed in State/Area'].astype(float),  # Column containing country names for hover information
    locationmode='USA-states', 
    scope='usa',color_continuous_scale='Reds',title="Unemplyment Rate of Various States of America"
)
fig.update_traces(marker_line_width=3,marker_line_color='white')
fig.update_layout(coloraxis_colorbar_title_text = 'Unemployment Rate')
fig.update_traces(
    customdata= np.stack((df_22['State/Area'], df_22['Percent of Labor Force Unemployed in State/Area']), axis=-1),
    hovertemplate=(
        "<b>%{customdata[0]}</b><br>"+\
        "Unemployment Rate: %{customdata[1]}"
    )
)

fig.show()

Exercise

Use the GDP data to plot the map of the USA.

• Plot the unemployment rate during June 2020 on the map of USA. Give hover text with state name and the unemployment rate. Give boundaries to each state, boundary color should be white with width 1. Use red color to scale the value. Give appropriate title to the plot and the colorbar. Use plotly

Choropleth of Inflation Rate of India

Now we will plot a choropleth using map of India. We use the inflation rate (CPI) data available in RBI website. The geojson file of India is downloaded https://un-mapped.carto.com/tables/states_india/public/map

import json
india=json.load(open('/home/stephy/Documents/class/Python/Sem 4/states_india.geojson'))
india['features'][0]
{'type': 'Feature',
 'geometry': {'type': 'MultiPolygon',
  'coordinates': [[[[78.34088, 19.883615],
     [78.351327, 19.88184],
     [78.370422, 19.883346],
     [78.379149, 19.879733],
     [78.388848, 19.879703],
     [78.389673, 19.874372],
     [78.388883, 19.864121],
     [78.390691, 19.856213],
     [78.390645, 19.853215],
     [78.39395, 19.846705],
     [78.402384, 19.836943],
     [78.413779, 19.830435],
     [78.433447, 19.8237],
     [78.449385, 19.819844],
     [78.469482, 19.816847],
     [78.481036, 19.817011],
     [78.489156, 19.807863],
     [78.494337, 19.799196],
     [78.498808, 19.793852],
     [78.508559, 19.793125],
     [78.514515, 19.801887],
     [78.517292, 19.814976],
     [78.52413, 19.820588],
     [78.531195, 19.822351],
     [78.562889, 19.81634],
     [78.57869, 19.814543],
     [78.590001, 19.81245],
     [78.596781, 19.816171],
     [78.600308, 19.818109],
     [78.608696, 19.818273],
     [78.6194, 19.814049],
     [78.624399, 19.809511],
     [78.640296, 19.804603],
     [78.653437, 19.802299],
     [78.691529, 19.790835],
     [78.701092, 19.790606],
     [78.706982, 19.78649],
     [78.714397, 19.773648],
     [78.721678, 19.767761],
     [78.729945, 19.769936],
     [78.737084, 19.773814],
     [78.743584, 19.780392],
     [78.748327, 19.780624],
     [78.757511, 19.778269],
     [78.768503, 19.776623],
     [78.778502, 19.780361],
     [78.790189, 19.779758],
     [78.802269, 19.769614],
     [78.808521, 19.761128],
     [78.812958, 19.759054],
     [78.822503, 19.761473],
     [78.830649, 19.761452],
     [78.843675, 19.763165],
     [78.84972, 19.760103],
     [78.851016, 19.759449],
     [78.858979, 19.739141],
     [78.862331, 19.732457],
     [78.862815, 19.720906],
     [78.867081, 19.707098],
     [78.868905, 19.697974],
     [78.869422, 19.687921],
     [78.868641, 19.680728],
     [78.864552, 19.670156],
     [78.864228, 19.657568],
     [78.883413, 19.658081],
     [78.896459, 19.656564],
     [78.90928, 19.665855],
     [78.921413, 19.669038],
     [78.940524, 19.663513],
     [78.949131, 19.666484],
     [78.959753, 19.664763],
     [78.967233, 19.660136],
     [78.97453, 19.650617],
     [78.97358, 19.640775],
     [78.970252, 19.630965],
     [78.968443, 19.618754],
     [78.971208, 19.605964],
     [78.976928, 19.598729],
     [78.983909, 19.591905],
     [78.986123, 19.586583],
     [78.983456, 19.581438],
     [78.974578, 19.574955],
     [78.970286, 19.571788],
     [78.960037, 19.564221],
     [78.955135, 19.558982],
     [78.960139, 19.553351],
     [78.970447, 19.551861],
     [78.979141, 19.551441],
     [78.98409, 19.552469],
     [78.991979, 19.558593],
     [78.998479, 19.563587],
     [79.001214, 19.562576],
     [79.005712, 19.56093],
     [79.017847, 19.545361],
     [79.024018, 19.541568],
     [79.044514, 19.546619],
     [79.057234, 19.546907],
     [79.065829, 19.540379],
     [79.069113, 19.532919],
     [79.079598, 19.530277],
     [79.1024, 19.532185],
     [79.108638, 19.529668],
     [79.108135, 19.521095],
     [79.110536, 19.514481],
     [79.118614, 19.511541],
     [79.132272, 19.50826],
     [79.146097, 19.502911],
     [79.16284, 19.487392],
     [79.174978, 19.469654],
     [79.183689, 19.46259],
     [79.194947, 19.460624],
     [79.22466, 19.466447],
     [79.228883, 19.469741],
     [79.226629, 19.477517],
     [79.214705, 19.486702],
     [79.212781, 19.493558],
     [79.214002, 19.500534],
     [79.221639, 19.512457],
     [79.229364, 19.518877],
     [79.237932, 19.522609],
     [79.242575, 19.528668],
     [79.244303, 19.534625],
     [79.245294, 19.543897],
     [79.244103, 19.55277],
     [79.24571, 19.561308],
     [79.256049, 19.57949],
     [79.254619, 19.588651],
     [79.249726, 19.595113],
     [79.243634, 19.601304],
     [79.243023, 19.611234],
     [79.249805, 19.614291],
     [79.261168, 19.612966],
     [79.273829, 19.608448],
     [79.299221, 19.598547],
     [79.310843, 19.591996],
     [79.315642, 19.583876],
     [79.320286, 19.567421],
     [79.327529, 19.573643],
     [79.335719, 19.575582],
     [79.346936, 19.575682],
     [79.375117, 19.573178],
     [79.377285, 19.563513],
     [79.386819, 19.557837],
     [79.395096, 19.550926],
     [79.407979, 19.541862],
     [79.419761, 19.535536],
     [79.429347, 19.534911],
     [79.436447, 19.537173],
     [79.447447, 19.5356],
     [79.464392, 19.521088],
     [79.471345, 19.511164],
     [79.476658, 19.499625],
     [79.486284, 19.505997],
     [79.499563, 19.507388],
     [79.51968, 19.520266],
     [79.522111, 19.524553],
     [79.522231, 19.538987],
     [79.521091, 19.54292],
     [79.521254, 19.544117],
     [79.532497, 19.548227],
     [79.54128, 19.550247],
     [79.548958, 19.550705],
     [79.551042, 19.544903],
     [79.551502, 19.536212],
     [79.554002, 19.528306],
     [79.558811, 19.522282],
     [79.571932, 19.513174],
     [79.579781, 19.508929],
     [79.589013, 19.505347],
     [79.597697, 19.504046],
     [79.604424, 19.506085],
     [79.607216, 19.510263],
     [79.608805, 19.51683],
     [79.609309, 19.52337],
     [79.613572, 19.541733],
     [79.617268, 19.551439],
     [79.622176, 19.560879],
     [79.627763, 19.567353],
     [79.646103, 19.577255],
     [79.659015, 19.578958],
     [79.671471, 19.579447],
     [79.684435, 19.57886],
     [79.691214, 19.581465],
     [79.702126, 19.583407],
     [79.711565, 19.583543],
     [79.719383, 19.585657],
     [79.736376, 19.598899],
     [79.746805, 19.605472],
     [79.76075, 19.60467],
     [79.77279, 19.602519],
     [79.784049, 19.60006],
     [79.791952, 19.595987],
     [79.792831, 19.59205],
     [79.799832, 19.582514],
     [79.811771, 19.579209],
     [79.817657, 19.576299],
     [79.823154, 19.571483],
     [79.826115, 19.563983],
     [79.827186, 19.554849],
     [79.829728, 19.546827],
     [79.8348, 19.539544],
     [79.862842, 19.519719],
     [79.870417, 19.516216],
     [79.876844, 19.507598],
     [79.884866, 19.503359],
     [79.908911, 19.49918],
     [79.917712, 19.495049],
     [79.924485, 19.488273],
     [79.930245, 19.480333],
     [79.939579, 19.471891],
     [79.943989, 19.469739],
     [79.946912, 19.467299],
     [79.951718, 19.461633],
     [79.952922, 19.453759],
     [79.952487, 19.443621],
     [79.965989, 19.428786],
     [79.976763, 19.412381],
     [79.979804, 19.403952],
     [79.979358, 19.394531],
     [79.977516, 19.385082],
     [79.974009, 19.377548],
     [79.973298, 19.363879],
     [79.971294, 19.353685],
     [79.963922, 19.337592],
     [79.964153, 19.327502],
     [79.970423, 19.309622],
     [79.968578, 19.300578],
     [79.955728, 19.291952],
     [79.95196, 19.287226],
     [79.938692, 19.262435],
     [79.926948, 19.229569],
     [79.926118, 19.218198],
     [79.929295, 19.206607],
     [79.942065, 19.187096],
     [79.949254, 19.179151],
     [79.950039, 19.173881],
     [79.947526, 19.167685],
     [79.932946, 19.164212],
     [79.929122, 19.160405],
     [79.912778, 19.145069],
     [79.877975, 19.12597],
     [79.869893, 19.119096],
     [79.864814, 19.10739],
     [79.866839, 19.094269],
     [79.867891, 19.063315],
     [79.8638, 19.04559],
     [79.866373, 19.038035],
     [79.877829, 19.038961],
     [79.887913, 19.044807],
     [79.897678, 19.048874],
     [79.909775, 19.051416],
     [79.922955, 19.050811],
     [79.932841, 19.045964],
     [79.939931, 19.03319],
     [79.941356, 19.025213],
     [79.939661, 18.999263],
     [79.938235, 18.978385],
     [79.939096, 18.975182],
     [79.942793, 18.961263],
     [79.947856, 18.941327],
     [79.948267, 18.919477],
     [79.947062, 18.900477],
     [79.947899, 18.881156],
     [79.945944, 18.862689],
     [79.940283, 18.848851],
     [79.932454, 18.841361],
     [79.92777, 18.840019],
     [79.911027, 18.838674],
     [79.900918, 18.834639],
     [79.916052, 18.810539],
     [79.923223, 18.801214],
     [79.938959, 18.788346],
     [79.947064, 18.785715],
     [79.957153, 18.783862],
     [79.988575, 18.778272],
     [79.999966, 18.777536],
     [80.01615, 18.772623],
     [80.019407, 18.770813],
     [80.021566, 18.764189],
     [80.023606, 18.761087],
     [80.025794, 18.755094],
     [80.036744, 18.7528],
     [80.039956, 18.750556],
     [80.053476, 18.725084],
     [80.060387, 18.721983],
     [80.069036, 18.72071],
     [80.072628, 18.717471],
     [80.075053, 18.712933],
     [80.081863, 18.706799],
     [80.086061, 18.705586],
     [80.089271, 18.706701],
     [80.091614, 18.705098],
     [80.094116, 18.701828],
     [80.107749, 18.689266],
     [80.113756, 18.681845],
     [80.11801, 18.680523],
     [80.123195, 18.680193],
     [80.125624, 18.683464],
     [80.128503, 18.685547],
     [80.138075, 18.68975],
     [80.143813, 18.691153],
     [80.149484, 18.690611],
     [80.155955, 18.697601],
     [80.159441, 18.699645],
     [80.165326, 18.700307],
     [80.169233, 18.6984],
     [80.171874, 18.694679],
     [80.174653, 18.684472],
     [80.178224, 18.681754],
     [80.182904, 18.680324],
     [80.196214, 18.68002],
     [80.19822, 18.683403],
     [80.198927, 18.688064],
     [80.201366, 18.692949],
     [80.209172, 18.701861],
     [80.215152, 18.704881],
     [80.221634, 18.705555],
     [80.227293, 18.703631],
     [80.23262, 18.700601],
     [80.237272, 18.701989],
     [80.24285, 18.704872],
     [80.248996, 18.704679],
     [80.25396, 18.705783],
     [80.267674, 18.718564],
     [80.274848, 18.722437],
     [80.278229, 18.71798],
     [80.285223, 18.715455],
     [80.296174, 18.69954],
     [80.303668, 18.690202],
     [80.306802, 18.683787],
     [80.315181, 18.656915],
     [80.322387, 18.640966],
     [80.326719, 18.626435],
     [80.338886, 18.601367],
     [80.34411, 18.595298],
     [80.346225, 18.592999],
     [80.349288, 18.591235],
     [80.355968, 18.593292],
     [80.360346, 18.595745],
     [80.363392, 18.598513],
     [80.364944, 18.603145],
     [80.367177, 18.606976],
     [80.369967, 18.608541],
     [80.374394, 18.609452],
     [80.377984, 18.607867],
     [80.389028, 18.598436],
     [80.396862, 18.604099],
     [80.400725, 18.607802],
     [80.409567, 18.612017],
     [80.423483, 18.613645],
     [80.425048, 18.615579],
     [80.457706, 18.631218],
     [80.470511, 18.627588],
     [80.475263, 18.627702],
     [80.479691, 18.628593],
     [80.487578, 18.632554],
     [80.490999, 18.633029],
     [80.495913, 18.631648],
     [80.505669, 18.62261],
     [80.51825, 18.620644],
     [80.521387, 18.618123],
     [80.522808, 18.612295],
     [80.525565, 18.60845],
     [80.529365, 18.605771],
     [80.536121, 18.598737],
     [80.536885, 18.594854],
     [80.539581, 18.591119],
     [80.548033, 18.588019],
     [80.565396, 18.577876],
     [80.569565, 18.574688],
     [80.57557, 18.566598],
     [80.589088, 18.562618],
     [80.603211, 18.554284],
     [80.60862, 18.550253],
     [80.611686, 18.545951],
     [80.618492, 18.538785],
     [80.621523, 18.531319],
     [80.628997, 18.528416],
     [80.634662, 18.523468],
     [80.640358, 18.524154],
     [80.642674, 18.520633],
     [80.644102, 18.512566],
     [80.6412, 18.508491],
     [80.642284, 18.50398],
     [80.644514, 18.499313],
     [80.65597, 18.484482],
     [80.658499, 18.477699],
     [80.670102, 18.471191],
     [80.673853, 18.468097],
     [80.677558, 18.466672],
     [80.680942, 18.463792],
     [80.681893, 18.461802],
     [80.688274, 18.459043],
     [80.690231, 18.455519],
     [80.69086, 18.450141],
     [80.699686, 18.445981],
     [80.702739, 18.44631],
     [80.706659, 18.441556],
     [80.703029, 18.440122],
     [80.700413, 18.434411],
     [80.698793, 18.42493],
     [80.720845, 18.411347],
     [80.726089, 18.415429],
     [80.730284, 18.415949],
     [80.734354, 18.414626],
     [80.736593, 18.409447],
     [80.734946, 18.405644],
     [80.731819, 18.400911],
     [80.723547, 18.392951],
     [80.724076, 18.388608],
     [80.726455, 18.384923],
     [80.730573, 18.381493],
     [80.732058, 18.37768],
     [80.730895, 18.373607],
     [80.730583, 18.36703],
     [80.734233, 18.35599],
     [80.742639, 18.344461],
     [80.750801, 18.327976],
     [80.752311, 18.322686],
     [80.75315, 18.310946],
     [80.752495, 18.308033],
     [80.759221, 18.303349],
     [80.760814, 18.297155],
     [80.764946, 18.290614],
     [80.767394, 18.288487],
     [80.771135, 18.287638],
     [80.774288, 18.284162],
     [80.781329, 18.270897],
     [80.78535, 18.26572],
     [80.789556, 18.265571],
     [80.792842, 18.264087],
     [80.796055, 18.26005],
     [80.796219, 18.255788],
     [80.795209, 18.252579],
     [80.792227, 18.24929],
     [80.777325, 18.242409],
     [80.764827, 18.23551],
     [80.741204, 18.224784],
     [80.740204, 18.21147],
     [80.740483, 18.20023],
     [80.742516, 18.177793],
     [80.753036, 18.170941],
     [80.770385, 18.169107],
     [80.794911, 18.170345],
     [80.805942, 18.17247],
     [80.818851, 18.18465],
     [80.818685, 18.189426],
     [80.825914, 18.19328],
     [80.826773, 18.198183],
     [80.825728, 18.218888],
     [80.826318, 18.225145],
     [80.825158, 18.231309],
     [80.826895, 18.237071],
     [80.832179, 18.239671],
     [80.837567, 18.240218],
     [80.842209, 18.239391],
     [80.84761, 18.234161],
     [80.867097, 18.23215],
     [80.869763, 18.227841],
     [80.875507, 18.213461],
     [80.875281, 18.209389],
     [80.87365, 18.207203],
     [80.869313, 18.204906],
     [80.864528, 18.204639],
     [80.857174, 18.206417],
     [80.856208, 18.203851],
     [80.856954, 18.19939],
     [80.860259, 18.189707],
     [80.860657, 18.184731],
     [80.859707, 18.174612],
     [80.863838, 18.157974],
     [80.863322, 18.153083],
     [80.863816, 18.149237],
     [80.866445, 18.145442],
     [80.869663, 18.142798],
     [80.870077, 18.139007],
     [80.889181, 18.138896],
     [80.901018, 18.140827],
     [80.908823, 18.141059],
     [80.920089, 18.144901],
     [80.940076, 18.1558],
     [80.943916, 18.158936],
     [80.944809, 18.161197],
     [80.952311, 18.163489],
     [80.957954, 18.170625],
     [80.962278, 18.173379],
     [80.973652, 18.174769],
     [80.981787, 18.174569],
     [80.981802, 18.173669],
     [80.981948, 18.164741],
     [80.982899, 18.158377],
     [80.980726, 18.150018],
     [80.975668, 18.144464],
     [80.957464, 18.129657],
     [80.955664, 18.123329],
     [80.957932, 18.110198],
     [80.957973, 18.104844],
     [80.956504, 18.097801],
     [80.954122, 18.09078],
     [80.952115, 18.087566],
     [80.960594, 18.078452],
     [80.971678, 18.056408],
     [80.974151, 18.050119],
     [80.970457, 18.038195],
     [80.970833, 18.037839],
     [80.972073, 18.031121],
     [80.974369, 18.025297],
     [80.976644, 18.018503],
     [80.978118, 18.009795],
     [80.979732, 18.004115],
     [80.980846, 17.999934],
     [80.982052, 17.983591],
     [80.986298, 17.956471],
     [80.98806, 17.952578],
     [80.988833, 17.946718],
     [80.99156, 17.941824],
     [80.992906, 17.9298],
     [81.000235, 17.926283],
     [81.002629, 17.92116],
     [81.010182, 17.871758],
     [81.012671, 17.845438],
     [81.020933, 17.823812],
     [81.040834, 17.796949],
     [81.046042, 17.793473],
     [81.053633, 17.791232],
     [81.064373, 17.790091],
     [81.074224, 17.791511],
     [81.082006, 17.793575],
     [81.087965, 17.802243],
     [81.095485, 17.81061],
     [81.09749, 17.818118],
     [81.106099, 17.824356],
     [81.117424, 17.830389],
     [81.129657, 17.835601],
     [81.167961, 17.859916],
     [81.184796, 17.856437],
     [81.192202, 17.856594],
     [81.200379, 17.854441],
     [81.221923, 17.840445],
     [81.228805, 17.836761],
     [81.236362, 17.833731],
     [81.262521, 17.819624],
     [81.271848, 17.817976],
     [81.306607, 17.817401],
     [81.323556, 17.819916],
     [81.33448, 17.820012],
     [81.343871, 17.818864],
     [81.353035, 17.819206],
     [81.366013, 17.81808],
     [81.376126, 17.815759],
     [81.38295, 17.813004],
     [81.392649, 17.811185],
     [81.393506, 17.814285],
     [81.402835, 17.815816],
     [81.411304, 17.813037],
     [81.417793, 17.812569],
     [81.422993, 17.81784],
     [81.42288, 17.824153],
     [81.427952, 17.828314],
     [81.434456, 17.829007],
     [81.44241, 17.823504],
     [81.446106, 17.823042],
     [81.467986, 17.832468],
     [81.47539, 17.832696],
     [81.47865, 17.830959],
     [81.481274, 17.825689],
     [81.483139, 17.817817],
     [81.487274, 17.812543],
     [81.489741, 17.810683],
     [81.494409, 17.810452],
     [81.50489, 17.814327],
     [81.51988, 17.814431],
     [81.534301, 17.818756],
     [81.562514, 17.824285],
     [81.573171, 17.833598],
     [81.577316, 17.835792],
     [81.583441, 17.835268],
     [81.602534, 17.827075],
     [81.608324, 17.823424],
     [81.619728, 17.823354],
     [81.624293, 17.83],
     [81.63257, 17.832422],
     [81.640037, 17.838433],
     [81.645141, 17.844273],
     [81.651362, 17.847275],
     [81.654721, 17.854853],
     [81.654582, 17.864236],
     [81.657627, 17.870429],
     [81.66127, 17.876146],
     [81.665508, 17.880774],
     [81.671622, 17.884875],
     [81.676616, 17.886371],
     [81.686324, 17.885548],
     [81.691804, 17.887448],
     [81.696846, 17.892765],
     [81.706457, 17.883538],
     [81.707846, 17.877748],
     [81.711753, 17.86917],
     [81.734904, 17.882144],
     [81.766859, 17.901407],
     [81.771994, 17.893468],
     [81.777441, 17.886796],
     [81.790041, 17.878842],
     [81.796141, 17.870666],
     [81.801371, 17.860696],
     [81.80301, 17.85235],
     [81.796461, 17.847438],
     [81.790972, 17.840155],
     [81.772556, 17.838112],
     [81.761563, 17.826503],
     [81.747524, 17.829768],
     [81.738208, 17.82747],
     [81.732037, 17.823374],
     [81.718138, 17.813266],
     [81.712619, 17.807723],
     [81.706865, 17.792675],
     [81.700035, 17.783893],
     [81.693428, 17.778749],
     [81.683318, 17.775291],
     [81.661641, 17.773767],
     [81.650537, 17.773777],
     [81.631697, 17.770911],
     [81.627223, 17.766693],
     [81.621459, 17.755061],
     [81.617846, 17.751772],
     [81.590595, 17.738779],
     [81.585273, 17.734212],
     [81.581615, 17.726798],
     [81.580634, 17.719972],
     [81.579743, 17.711055],
     [81.579837, 17.696059],
     [81.576871, 17.691029],
     [81.568863, 17.684841],
     [81.562329, 17.674598],
     [81.554775, 17.658921],
     [81.553014, 17.653537],
     [81.542859, 17.638666],
     [81.527653, 17.62704],
     [81.520585, 17.618131],
     [81.517981, 17.611822],
     [81.517991, 17.606609],
     [81.516625, 17.603601],
     [81.511659, 17.598163],
     [81.521408, 17.59127],
     [81.529645, 17.584079],
     [81.532635, 17.580956],
     [81.532483, 17.578061],
     [81.529045, 17.572967],
     [81.522492, 17.565619],
     [81.51786, 17.557691],
     [81.51019, 17.547736],
     [81.508232, 17.539928],
     [81.506802, 17.518502],
     [81.508294, 17.509989],
     [81.50697, 17.498473],
     [81.502419, 17.485508],
     [81.502554, 17.47451],
     [81.504675, 17.468895],
     [81.504182, 17.462346],
     [81.502254, 17.457368],
     [81.490831, 17.440763],
     [81.48108, 17.428094],
     [81.466102, 17.413975],
     [81.460408, 17.406913],
     [81.454946, 17.399276],
     [81.450374, 17.390304],
     [81.442506, 17.383535],
     [81.428978, 17.380762],
     [81.427171, 17.37497],
     [81.42452, 17.370226],
     [81.415321, 17.369997],
     [81.394075, 17.363695],
     [81.380661, 17.365717],
     [81.372819, 17.368787],
     [81.358677, 17.377003],
     [81.35214, 17.38418],
     [81.35175, 17.393855],
     [81.346159, 17.398427],
     [81.339158, 17.400511],
     [81.333519, 17.399058],
     [81.331284, 17.397554],
     [81.320446, 17.383203],
     [81.310444, 17.36816],
     [81.307651, 17.360583],
     [81.298019, 17.346008],
     [81.290635, 17.338218],
     [81.276024, 17.32846],
     [81.271078, 17.330387],
     [81.266364, 17.331096],
     [81.243124, 17.331224],
     [81.227564, 17.330119],
     [81.199113, 17.335696],
     [81.190355, 17.330549],
     [81.187018, 17.322849],
     [81.180781, 17.313797],
     [81.179435, 17.304818],
     [81.179976, 17.27508],
     [81.183492, 17.268801],
     [81.189435, 17.263057],
     [81.176876, 17.250775],
     [81.171659, 17.241052],
     [81.154413, 17.240102],
     [81.127033, 17.233013],
     [81.093337, 17.222025],
     [81.071047, 17.212469],
     [81.062682, 17.218493],
     [81.055379, 17.222152],
     [81.052712, 17.212881],
     [81.04879, 17.208289],
     [81.043677, 17.2056],
     [81.040311, 17.2007],
     [81.03501, 17.195019],
     [81.014238, 17.192776],
     [81.006552, 17.190325],
     [81.000102, 17.188013],
     [80.993982, 17.187695],
     [80.975138, 17.192801],
     [80.964805, 17.196452],
     [80.956537, 17.201548],
     [80.947837, 17.208569],
     [80.939438, 17.213195],
     [80.929422, 17.213982],
     [80.922617, 17.213403],
     [80.914123, 17.209422],
     [80.916409, 17.201019],
     [80.915777, 17.192017],
     [80.917262, 17.186225],
     [80.92183, 17.181984],
     [80.923669, 17.178053],
     [80.922798, 17.168995],
     [80.920999, 17.161742],
     [80.922126, 17.1542],
     [80.918194, 17.150702],
     [80.912788, 17.149399],
     [80.904955, 17.14902],
     [80.89766, 17.149278],
     [80.885557, 17.153394],
     [80.879102, 17.154499],
     [80.875606, 17.132837],
     [80.872282, 17.126231],
     [80.863857, 17.119642],
     [80.873039, 17.104092],
     [80.871239, 17.09392],
     [80.871396, 17.089831],
     [80.865311, 17.076834],
     [80.867722, 17.059203],
     [80.863783, 17.0524],
     [80.859411, 17.048514],
     [80.853738, 17.041195],
     [80.841357, 17.034061],
     [80.838161, 17.040679],
     [80.832093, 17.046094],
     [80.812057, 17.050818],
     [80.801674, 17.0513],
     [80.794126, 17.055859],
     [80.789018, 17.057026],
     [80.774916, 17.058501],
     [80.755389, 17.063085],
     [80.734502, 17.072726],
     [80.721374, 17.073553],
     [80.703916, 17.07641],
     [80.692741, 17.076777],
     [80.69014, 17.072354],
     [80.684385, 17.069317],
     [80.676491, 17.067956],
     [80.669331, 17.064755],
     [80.657916, 17.06259],
     [80.648319, 17.063124],
     [80.64265, 17.066869],
     [80.646447, 17.073585],
     [80.646477, 17.079907],
     [80.653086, 17.085067],
     [80.664115, 17.09194],
     [80.66537, 17.095729],
     [80.654952, 17.102415],
     [80.645095, 17.104907],
     [80.633895, 17.109634],
     [80.638417, 17.117903],
     [80.632574, 17.119752],
     [80.625206, 17.123554],
     [80.621561, 17.12448],
     [80.612511, 17.123342],
     [80.603742, 17.127378],
     [80.58886, 17.138722],
     [80.582096, 17.143039],
     [80.568776, 17.145757],
     [80.55766, 17.131351],
     [80.550258, 17.12814],
     [80.544002, 17.127511],
     [80.537777, 17.121373],
     [80.528014, 17.115865],
     [80.520308, 17.11392],
     [80.504691, 17.115547],
     [80.505143, 17.108991],
     [80.500987, 17.100145],
     [80.497314, 17.095037],
     [80.495383, 17.090615],
     [80.493222, 17.075329],
     [80.493271, 17.064074],
     [80.490807, 17.058036],
     [80.483679, 17.050577],
     [80.480363, 17.045867],
     [80.472841, 17.044947],
     [80.461485, 17.044784],
     [80.454854, 17.024794],
     [80.441246, 17.029516],
     [80.429112, 17.03258],
     [80.426358, 17.036312],
     [80.422856, 17.049469],
     [80.409422, 17.061823],
     [80.407433, 17.067403],
     [80.408301, 17.072857],
     [80.40588, 17.081479],
     [80.403609, 17.08515],
     [80.401721, 17.085386],
     [80.398629, 17.083777],
     [80.387577, 17.073727],
     [80.382676, 17.070682],
     [80.375506, 17.064142],
     [80.376273, 17.060287],
     [80.380151, 17.055124],
     [80.386922, 17.041756],
     [80.385777, 17.035512],
     [80.386442, 17.03099],
     [80.392324, 17.027432],
     [80.39727, 17.02175],
     [80.396465, 17.014998],
     [80.395071, 17.010644],
     [80.390531, 17.00774],
     [80.37976, 17.003532],
     [80.379781, 17.000842],
     [80.380187, 16.994826],
     [80.378297, 16.990661],
     [80.36964, 16.982175],
     [80.366835, 16.978346],
     [80.369005, 16.978224],
     [80.389416, 16.970278],
     [80.406253, 16.96481],
     [80.420984, 16.962005],
     [80.451205, 16.952831],
     [80.461775, 16.946255],
     [80.471934, 16.938163],
     [80.478572, 16.934482],
     [80.485588, 16.929158],
     [80.49655, 16.925455],
     [80.494306, 16.935038],
     [80.495661, 16.94484],
     [80.500003, 16.956149],
     [80.518497, 16.950444],
     [80.529429, 16.944434],
     [80.540479, 16.958194],
     [80.578271, 16.935019],
     [80.592961, 16.934674],
     [80.597948, 16.924852],
     [80.599112, 16.919823],
     [80.59817, 16.915321],
     [80.594701, 16.912516],
     [80.590923, 16.911291],
     [80.590047, 16.904979],
     [80.592182, 16.898834],
     [80.594604, 16.895383],
     [80.595587, 16.882191],
     [80.585334, 16.881334],
     [80.573776, 16.87727],
     [80.573681, 16.85817],
     [80.572847, 16.850844],
     [80.57128, 16.844599],
     [80.563451, 16.83308],
     [80.564884, 16.826711],
     [80.570121, 16.825445],
     [80.575879, 16.825427],
     [80.58066, 16.820444],
     [80.594751, 16.815037],
     [80.602563, 16.806323],
     [80.606696, 16.800453],
     [80.613027, 16.796197],
     [80.606454, 16.79352],
     [80.578243, 16.779164],
     [80.571719, 16.771243],
     [80.555415, 16.77604],
     [80.538956, 16.778411],
     [80.52697, 16.77711],
     [80.519873, 16.778263],
     [80.498399, 16.784145],
     [80.493499, 16.789573],
     [80.487884, 16.7932],
     [80.47987, 16.796548],
     [80.465219, 16.798233],
     [80.463884, 16.814644],
     [80.461392, 16.823436],
     [80.453547, 16.827515],
     [80.440608, 16.831951],
     [80.440875, 16.835273],
     [80.431608, 16.837388],
     [80.43016, 16.845176],
     [80.427413, 16.850368],
     [80.408431, 16.857746],
     [80.408912, 16.845074],
     [80.407884, 16.83961],
     [80.402965, 16.833201],
     [80.388137, 16.827559],
     [80.386579, 16.820516],
     [80.384615, 16.818667],
     [80.381675, 16.819127],
     [80.375818, 16.834748],
     [80.373423, 16.846868],
     [80.369894, 16.858319],
     [80.367332, 16.862835],
     [80.3596, 16.861222],
     [80.34361, 16.863631],
     [80.345961, 16.874954],
     [80.335965, 16.878865],
     [80.327863, 16.878773],
     [80.327078, 16.88526],
     [80.324614, 16.891799],
     [80.324749, 16.902448],
     [80.321882, 16.919752],
     [80.323978, 16.920141],
     [80.316433, 16.931544],
     [80.312513, 16.947162],
     [80.309922, 16.963517],
     [80.2975, 16.9844],
     [80.284675, 17.000793],
     [80.270646, 17.018123],
     [80.258733, 17.01486],
     [80.249595, 17.010287],
     [80.24419, 17.0098],
     [80.240685, 17.012377],
     [80.226255, 17.027043],
     [80.221639, 17.026584],
     [80.209667, 17.020538],
     [80.203954, 17.025418],
     [80.204508, 17.033055],
     [80.20264, 17.049843],
     [80.190479, 17.052245],
     [80.178905, 17.037718],
     [80.175553, 17.030949],
     [80.157473, 17.01537],
     [80.15083, 17.008807],
     [80.148945, 17.000621],
     [80.142235, 16.992704],
     [80.135265, 16.992661],
     [80.128501, 16.994131],
     [80.123028, 16.993182],
     [80.121138, 16.989636],
     [80.114134, 16.988621],
     [80.107808, 16.990085],
     [80.096009, 16.9947],
     [80.092277, 16.984961],
     [80.092586, 16.970492],
     [80.054171, 16.972538],
     [80.045375, 16.94374],
     [80.036808, 16.934686],
     [80.026903, 16.926089],
     [80.017863, 16.914541],
     [80.011079, 16.89877],
     [80.003373, 16.884622],
     [80.000017, 16.870579],
     [80.002475, 16.869516],
     [80.022216, 16.860326],
     [80.0415, 16.859862],
     [80.042754, 16.855186],
     [80.042794, 16.846561],
     [80.045086, 16.839849],
     [80.050258, 16.833534],
     [80.07813, 16.821152],
     [80.073397, 16.810059],
     [80.06613, 16.789458],
     [80.060073, 16.778879],
     [80.0564, 16.752791],
     [80.051846, 16.747336],
     [80.041254, 16.739402],
     [80.031812, 16.726115],
     [80.025171, 16.719469],
     [80.010876, 16.710578],
     [80.002156, 16.702075],
     [80.000012, 16.70018],
     [79.981524, 16.667181],
     [79.966655, 16.649711],
     [79.951061, 16.640285],
     [79.93213, 16.638374],
     [79.922285, 16.640748],
     [79.910529, 16.645427],
     [79.899336, 16.657106],
     [79.889049, 16.68365],
     [79.881671, 16.696152],
     [79.870192, 16.705634],
     [79.852891, 16.703855],
     [79.834241, 16.698843],
     [79.818561, 16.697832],
     [79.791606, 16.732085],
     [79.783121, 16.736741],
     [79.771314, 16.734339],
     [79.758693, 16.727323],
     [79.742004, 16.705228],
     [79.730935, 16.699809],
     [79.691195, 16.697522],
     [79.676786, 16.69113],
     [79.663644, 16.681218],
     [79.656176, 16.669137],
     ...]]]},
 'properties': {'cartodb_id': 1, 'state_code': 0, 'st_nm': 'Telangana'}}

We’ll now proceed to utilize the inflation data collected from the RBI website. This data is stored in a CSV file, which we can conveniently access using the Pandas library in Python.

import pandas as pd
data=pd.read_csv('/home/stephy/Documents/class/Python/Sem 4/inflationIndia.csv')
data.shape
(35, 10)
data.columns
Index(['State/Union Territory', '2014-15', '2015-16', '2016-17', '2017-18',
       '2018-19', '2019-20', '2020-21', '2021-22', '2022-23'],
      dtype='object')
data['2022-23'].dtype
dtype('float64')

Now we need to assign an identifier which will connect the states in json file with that of CSV file.

india['features'][0]['properties']
{'cartodb_id': 1, 'state_code': 0, 'st_nm': 'Telangana'}
state_id_map={}
for feature in india['features']:
  feature["id"]=feature['properties']['state_code']
  state_id_map[feature['properties']['st_nm']]=feature["id"]
state_id_map
{'Telangana': 0,
 'Andaman & Nicobar Island': 35,
 'Andhra Pradesh': 28,
 'Arunanchal Pradesh': 12,
 'Assam': 18,
 'Bihar': 10,
 'Chhattisgarh': 22,
 'Daman & Diu': 25,
 'Goa': 30,
 'Gujarat': 24,
 'Haryana': 6,
 'Himachal Pradesh': 2,
 'Jammu & Kashmir': 1,
 'Jharkhand': 20,
 'Karnataka': 29,
 'Kerala': 32,
 'Lakshadweep': 31,
 'Madhya Pradesh': 23,
 'Maharashtra': 27,
 'Manipur': 14,
 'Chandigarh': 4,
 'Puducherry': 34,
 'Punjab': 3,
 'Rajasthan': 8,
 'Sikkim': 11,
 'Tamil Nadu': 33,
 'Tripura': 16,
 'Uttar Pradesh': 9,
 'Uttarakhand': 5,
 'West Bengal': 19,
 'Odisha': 21,
 'Dadara & Nagar Havelli': 26,
 'Meghalaya': 17,
 'Mizoram': 15,
 'Nagaland': 13,
 'NCT of Delhi': 7}

Now we will create a new column in data file which will be the identifier.

data['ID']=data['State/Union Territory'].apply(lambda x: state_id_map[x])

Now we will proceed with plotting our choropleth map.

import plotly.express as px
fig=px.choropleth(data,locations='ID',geojson=india,color='2022-23')
fig.show()

Now we have the choropleth is shown on a very wide canvas. Now we will reduce the canvas to continent Asia.

import plotly.express as px
fig=px.choropleth(data,locations='ID',geojson=india,color='2022-23',scope='asia')
fig.show()

Currently, the choropleth canvas extends beyond the regions with available data. To ensure a more precise visual representation, let’s adjust the canvas size to exactly match the area covered by our data. This will eliminate unnecessary blank space and provide a focused view on the data points.

import plotly.express as px
fig=px.choropleth(data,locations='ID',geojson=india,color='2022-23')
fig.update_geos(fitbounds='locations',visible=False)
fig.show()

Now we can see that the hovering text is meaningless. Hence in order to make it meaningful we will introduce the state name and the values of the inflation rate in a meaningful form.

fig=px.choropleth(data,locations='ID',geojson=india,color='2022-23')
fig.update_geos(fitbounds='locations',visible=False)

fig.update_traces(
    customdata= np.stack((data['State/Union Territory'], data['2022-23']), axis=-1),
    hovertemplate=(
        "<b>%{customdata[0]}</b><br>"+\
        "Inflation Rate 2022-23: %{customdata[1]}"
    )
)
fig.show()
fig=px.choropleth(data,locations='ID',geojson=india,color='2022-23')
fig.update_geos(fitbounds='locations',visible=False)

fig.update_traces(
    customdata= np.stack((data['State/Union Territory'], data['2022-23'],data['2021-22']), axis=-1),
    hovertemplate=(
        "<b>%{customdata[0]}</b><br>"+
        "Inflation Rate 2022-23: %{customdata[1]}<br>"+
         "Inflation Rate 2021-22: %{customdata[2]}"
    )
)
fig.show()

Now lets edit the title of colour bar (legend).

fig=px.choropleth(data,locations='ID',geojson=india,color='2022-23')
fig.update_geos(fitbounds='locations',visible=False)

fig.update_traces(
    customdata= np.stack((data['State/Union Territory'], data['2022-23'],data['2021-22']), axis=-1),
    hovertemplate=(
        "<b>%{customdata[0]}</b><br>"+
        "Inflation Rate 2022-23: %{customdata[1]}<br>"+
         "Inflation Rate 2021-22: %{customdata[2]}"
    )
)
fig.update_layout(coloraxis_colorbar_title_text = 'Inflation Rate 2022-23')
fig.show()

Exercise

Plot the population density on various district of Kerala on the map of Kerala.

Choropleth of Unemployment Rate of Countries for Various Years

Next, we will plot the global unemployment rates of various countries on a world map. The data has been sourced from Kaggle and is accessible via the provided link.

import pandas as pd
import plotly.express as px
data=pd.read_csv('/home/stephy/Documents/class/Python/Sem 4/unemploymentWorld.csv')
data.head(2)
Country Name Country Code 1991 1992 1993 1994 1995 1996 1997 1998 ... 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
0 Africa Eastern and Southern AFE 7.80 7.84 7.85 7.84 7.83 7.84 7.86 7.81 ... 6.56 6.45 6.41 6.49 6.61 6.71 6.73 6.91 7.56 8.11
1 Afghanistan AFG 10.65 10.82 10.72 10.73 11.18 10.96 10.78 10.80 ... 11.34 11.19 11.14 11.13 11.16 11.18 11.15 11.22 11.71 13.28

2 rows × 33 columns

data.columns
Index(['Country Name', 'Country Code', '1991', '1992', '1993', '1994', '1995',
       '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004',
       '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013',
       '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021'],
      dtype='object')

Now let us consider plotting the unemployment rate of various countries during the year 2021.

fig=px.choropleth(data,locations='Country Code',color='2021')
fig.show()

Note that given dataset do not have data on green land. We can see that for most of the states the colours are not different as we have taken a vast range of colour variations. We confine our range of variation to be as (0,15)

fig=px.choropleth(data,locations='Country Code',color='2021',range_color=(0,15))
fig.show()

Though we attained colour variability between various countries, all the countries having employment over 15 are marked with same colour now.

import numpy as np
fig=px.choropleth(data,locations='Country Code',color='2021',range_color=(0,20), color_continuous_scale='Reds',labels={'2021':'Unemployment Rate'})
fig.update_layout(coloraxis_colorbar_title_text = 'Unemployment Rate')
fig.update_traces(marker_line_width=2,marker_line_color='white')
fig.update_traces(
    customdata= np.stack((data['Country Name'], data['2021']), axis=-1),
    hovertemplate=(
        "<b>%{customdata[0]}</b><br>"+\
        "Unemployment Rate: %{customdata[1]}"
    )
)
# Show the map
fig.show()
data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 235 entries, 0 to 234
Data columns (total 33 columns):
 #   Column        Non-Null Count  Dtype  
---  ------        --------------  -----  
 0   Country Name  235 non-null    object 
 1   Country Code  235 non-null    object 
 2   1991          235 non-null    float64
 3   1992          235 non-null    float64
 4   1993          235 non-null    float64
 5   1994          235 non-null    float64
 6   1995          235 non-null    float64
 7   1996          235 non-null    float64
 8   1997          235 non-null    float64
 9   1998          235 non-null    float64
 10  1999          235 non-null    float64
 11  2000          235 non-null    float64
 12  2001          235 non-null    float64
 13  2002          235 non-null    float64
 14  2003          235 non-null    float64
 15  2004          235 non-null    float64
 16  2005          235 non-null    float64
 17  2006          235 non-null    float64
 18  2007          235 non-null    float64
 19  2008          235 non-null    float64
 20  2009          235 non-null    float64
 21  2010          235 non-null    float64
 22  2011          235 non-null    float64
 23  2012          235 non-null    float64
 24  2013          235 non-null    float64
 25  2014          235 non-null    float64
 26  2015          235 non-null    float64
 27  2016          235 non-null    float64
 28  2017          235 non-null    float64
 29  2018          235 non-null    float64
 30  2019          235 non-null    float64
 31  2020          235 non-null    float64
 32  2021          235 non-null    float64
dtypes: float64(31), object(2)
memory usage: 60.7+ KB

Now we will try to show the unemployment rate for the various years in a single choropleth using animation. For that we have to create a new variable representing the years and other the respective values. Hence we will like to transfer the dimension of data now.

data1=data.melt(id_vars=['Country Name','Country Code'], var_name='Year', value_name='Unemployment Rate')
data1.head()
Country Name Country Code Year Unemployment Rate
0 Africa Eastern and Southern AFE 1991 7.80
1 Afghanistan AFG 1991 10.65
2 Africa Western and Central AFW 1991 4.42
3 Angola AGO 1991 4.21
4 Albania ALB 1991 10.31

Now we will bring animation into the choropleth to reflect the changes in the unemployment rate.

import numpy as np
fig=px.choropleth(data1,locations='Country Code',color='Unemployment Rate',animation_frame='Year', range_color=(0,20), color_continuous_scale='Reds',labels={'2021':'Unemployment Rate'})
fig.update_layout(coloraxis_colorbar_title_text = 'Unemployment Rate')
fig.update_layout(
  updatemenus=[
    dict(
      buttons=[
        dict(label='Play',
             method='animate',
             args=[None, {'frame': {'duration': 1000}}])  # Adjust duration here
      ]
    )
  ]
)

fig.update_traces(marker_line_width=2,marker_line_color='white')
fig.update_traces(
    customdata= np.stack((data['Country Name'], data['2021']), axis=-1),
    hovertemplate=(
        "<b>%{customdata[0]}</b><br>"+\
        "Unemployment Rate: %{customdata[1]}"
    )
)
# Show the map
fig.show()

Exercise

Plot the unemployment data for 2020 on a world map and colour the borders with green with thickness 3. Give the blue colour variation.

Map the unemployment data from 2010-2020 into a world map using animations.

Plot the number of deaths per 1 million people due to covid using the data available in Asian States available with the website : www.worldometers.info. Use the country code available at the link.

Choropleth with Discrete Colours

We want to map the seats bagged by the major alliances - National Democratic Alliance and United Progressive Alliance in the general elections 2019. The seats secured by each party is avaliable at www.indiavotes.com. Alternatively we can collect the list of candidates won from various states from ECI website. The list of parties in each coalition is given in https://carnegieendowment.org.

On the map of india, colour (Safron for NDA, Blue for UPA and Green for others) each state with the coalition that secured maximum seats from the state.