Adventures in Plotly: An Interactive Choropleth Map
It’s no secret that the key to interesting data science is keeping all the data science hidden away behind beautiful and easy-to-understand visualizations. With this in mind and a project in the works, a task was born: illustrate the change in forest coverage over time.
A note: you can see my full code on Github.
I set out, confident and excited, ready to whip up a quick choropleth map and move on to other things. But alas, this was not to be.
The first thing I did was take the data, a beautiful set from The World Bank, and clean it up a bit. This data was incredibly complete — it had the square kilometers of forest coverage for all 195 countries for every year from 1990 to 2016 — but there were a few columns I didn’t need, and a few rows which held data for multiple countries, rather than individual ones.
Once I’d removed what needed to be removed, I moved on to the future predictions. My final visualization only held data for up to the year 2020 due to size restrictions, but I wanted to engineer the data for up to the year 2050. Instead of using a predictive model to do this, I took the average annual rate of change from the years 2006–2016 and used that number to predict what 2017–2050 would look like if forest coverage changes continued at a consistent rate.
Cleaning and engineering out of the way, I had one step left before I created my visualization. I took my 70-column dataframe and melted it down so it had four columns instead: Country Name, Country Code, variable (the year), and value (square km of forest coverage). This ended up being an essential step to making my visualization work the way I wanted it to, and all it took was a couple lines of code!
Finally, the step you’ve all been waiting for: the visualization.
Let’s start by looking at the end product:
A few things to notice:
- Slider bar that transitions between different years
- Color changes based on appropriate forest coverage and country
- Countries have an interactive element (hovering over them displays relevant information)
We begin as follows:
data_slider = []for year in df['variable'].unique():
df_year = df[df['variable'] == year]
for col in df_year.columns:
df_year[col] = df_year[col].astype(str)
data_one_year = dict(
type='choropleth',
locations = df_year['Country Name'],
z=df_year['value'].astype(float),
locationmode='country names',
colorscale = "greens",
)
data_slider.append(data_one_year)
The list we’ll use to populate our slider is established, and then we fill it with data, one year at a time.
Next, we’ll actually build the slider object:
steps = []
for i in range(len(data_slider)):
step = dict(method='restyle',
args=['visible', [False] * len(data_slider)],
label='Year {}'.format(i + 1960))
step['args'][1][i] = True
steps.append(step)
sliders = [dict(active=0, pad={"t": 1}, steps=steps)] And set up the layout for the image:
layout = dict(geo=dict(scope='world',
showcountries = True,
projection={'type': 'equirectangular'}),
sliders=sliders)Finally, we put the whole image together and plot it!
fig = dict(data=data_slider, layout=layout)plotly.offline.iplot(fig, show_link = True)
And that’s all there is to it! Go forth and code some choropleth maps!

