Geographical Plotting

Exploration of geographical data visualization with choropleth maps

December 05, 2020 · 9 mins read

A Choropleth Map is a map composed of colored polygons. It is used to represent spatial variations of a quantity. The following simple exercises help in getting familiar with visualizing geographical data.

Refer to Plotly documentation for upto date usage and best practices guides.


Imports

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

Basic Configuration

Data is in JSON dictionary format. The following example shows how a data dictionary is setup. The keyword arg z contains the data to be visualized. Here is a simple example below.

data = dict(type = 'choropleth',
            locations = ['AZ','CA','NY'],
            locationmode = 'USA-states',
            colorscale= 'Jet',
            text= ['Arizona','California','NewYork'],
            z=[1.0,2.0,3.0],
            colorbar = {'title':'Values'})

layout = dict(geo = {'scope':'usa'})
choromap = go.Figure(data = [data],layout = layout)
iplot(choromap)

Import Pandas to read in datasets

import pandas as pd
  • Now onto some real world datasets

US Agricultural exports 2011

# Read in the data
df = pd.read_csv('2011_US_AGRI_Exports')

# Get basic information on the data
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 50 entries, 0 to 49
Data columns (total 18 columns):
 #   Column         Non-Null Count  Dtype
---  ------         --------------  -----
 0   code           50 non-null     object
 1   state          50 non-null     object
 2   category       50 non-null     object
 3   total exports  50 non-null     float64
 4   beef           50 non-null     float64
 5   pork           50 non-null     float64
 6   poultry        50 non-null     float64
 7   dairy          50 non-null     float64
 8   fruits fresh   50 non-null     float64
 9   fruits proc    50 non-null     float64
 10  total fruits   50 non-null     float64
 11  veggies fresh  50 non-null     float64
 12  veggies proc   50 non-null     float64
 13  total veggies  50 non-null     float64
 14  corn           50 non-null     float64
 15  wheat          50 non-null     float64
 16  cotton         50 non-null     float64
 17  text           50 non-null     object
dtypes: float64(14), object(4)
memory usage: 7.2+ KB

Create data variable from the dataset.

Note: Observe locations as passed in via the code column and locationmode is set to USA-states

exports = dict(type='choropleth',
           colorscale='ylorbr',
           locations = df['code'],
           locationmode = 'USA-states',
           z = df['total exports'],
           text = df['text'],
           colorbar = {'title': 'Millions USD'},
           marker = dict(line = dict(color = 'rgb(82, 82, 82)',width = 1))
           )

Create layout variable for the dataset

Note: The layout scope is set to usa

layout = dict(title = '2011 US Agriculture Exports by State',
              geo = dict(scope='usa',
                         showlakes = True,
                         lakecolor = 'rgb(85,173,240)')
             )
choromap = go.Figure(data = [exports],layout = layout)
iplot(choromap)

Global GDP 2014

# Read in data
df = pd.read_csv('2014_World_GDP')
# Get basic info of the data
df.info
<bound method DataFrame.info of             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
..              ...             ...  ...
217  Virgin Islands            5.08  VGB
218       West Bank            6.64  WBG
219           Yemen           45.45  YEM
220          Zambia           25.61  ZMB
221        Zimbabwe           13.74  ZWE

[222 rows x 3 columns]>

Create data variable from the dataset.

Note: Observe locations as passed in via the code column and the default locationmode is global

gdp = dict(type='choropleth',
           locations = df['CODE'],
           z = df['GDP (BILLIONS)'],
           text = df['COUNTRY'],
           colorscale='ylorbr',
           colorbar = {'title': 'GDP in Billions USD'})

Create layout variable for the dataset

layout = dict(title = '2014 Global GDP',
              geo = dict(showframe = False,
                         projection = {'type': 'natural earth'}
             ))
choromap = go.Figure(data=[gdp],layout = layout)
iplot(choromap)

Global Power Consumption 2014

# Read in the data
power = pd.read_csv('2014_World_Power_Consumption.csv')

# Get some basic info on the data
power.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 212 entries, 0 to 211
Data columns (total 4 columns):
 #   Column                 Non-Null Count  Dtype
---  ------                 --------------  -----
 0   Code                   212 non-null    object
 1   Country                212 non-null    object
 2   Power Consumption KWH  212 non-null    float64
 3   Text                   212 non-null    object
dtypes: float64(1), object(3)
memory usage: 6.8+ KB

Create data variable from the dataset.

gdp = dict(type='choropleth',
           locations = power['Code'],
           z = power['Power Consumption KWH'],
           text = power['Text'],
           colorscale='ylorbr',
           colorbar = {'title': 'Global Power Consumption 2014 (WH)'})

Create layout variable for the dataset

layout = dict(title = 'Global Power Consumption 2014',
              geo = dict(showframe = False,
                         projection = {'type': 'natural earth'}
             ))
choromap = go.Figure(data=[gdp],layout = layout)
iplot(choromap)

Reference jupyter notebook.

Choropleth - Geographical Plotting