Day (8) — Data Visualization — How to use Choropleth maps

Keith Brooks
4 min readMar 9, 2018

--

“A close-up of a golden rim running across a globe” by chuttersnap on Unsplash

This article covers work from the Python for Data Science and Machine Learning Bootcamp course on Udemy by Jose Portilla and helpful tips along the way. This course was very helpful in gaining a base understanding of the topic.

The main theme around this data visualization section has been around telling a numerical story better. We have learned about built-in pandas data visualizations for a quick assessment. That was nice, but then we learned about the seaborn library and the information became more engaging. After that, we discovered plotly and the possible benefits of a more hands-on approach. Now we couple that with generating choropleth maps, with plotly. These maps enable us to visualize a measurement across a national or global focus. This is really nice when plotting information across regions to observe unique insights.

Topics:

  • What is the process for creating choropleth maps
  • How to generate data object for choropleth maps
  • How to generate layout object for choropleth maps
  • How to plot national choropleth maps
  • How to plot global choropleth maps

The Setup:
* The example uses Python 3.6 within Jupyter notebook with the below dependencies. Note: If error on notebook occurs use the following in the terminal to increase the limit.

jupyter notebook --NotebookApp.iopub_data_rate_limit=214748364

Pandas 0.20.3
Plotly 2.4.1
Cufflinks 0.12.1

Warning:
* Feel free to review the docs for additional arguments for the methods.

Choropleth Maps Process:
We will use python and plotly to generate the choropleth maps. The choropleth maps take two objects, (1) the data object and (2) the layout object. Once this information has been populated, we will create a Figure of the map and then plot using the iplot() method.

Generating data object, specific to national maps:
We may use six arguments to pass into the data object using the dict() method.

# colorscale → This is the color for the geographical map elements# locations → This is the data for the state abbreviations# locationmode → This lets plotly know we what nation to use# z → This is the numerical measurement for each state element; This should be of the same index sequence as the locations argument# text → This is the categorical value for each element# colorbar → Title for right side bar

Generating a layout object, specific to national maps:
We may use two arguments to pass into the layout object using the dict() method.

# title argument references the map title# geo argument references to the geographical map of interest

Generate a national choropleth map:
Now we use the go.Figure() method to generate an object for the map before we plot using the iplot() method or plot() method. The iplot() method will generate the map within the Jupyter Notebook, while the plot() method will open a new tab in the browser for a html file.

Generate a global choropleth map:
This follows the same procedure: (1) import data via desired format, (2) create data object, (3) create layout object, (4) create Figure object and finally (5) plot the data.

Well, that covers the Data Visualization portion…Now on to the machine learning. What trouble can we get into with that?

“The universe is change; our life is what our thoughts make it.” ~ Marcus Aurelius

--

--

Keith Brooks

Hi…This is intended to document my journey on my path to data science. I am a process oriented individual who loves to try new things and continue to learn.