cft

Analyze global COVID 19 data with Choropleth maps

Comparison with Plotly and Folium in Python


user

Mythili Krishnan

3 years ago | 5 min read

COVID-19 needs no introduction- it is the latest infectious disease that has gripped the whole world. So, have you wondered how the world has changed over this past few months? Can we visualize this change over months across different countries?

Too many questions but we have a simple answer — we can easily do this using Choropleth maps and I will make it simple with minimum coding requirements.

So without further ado let’s get started:

This will be our agenda today:

  1. What is a Choropleth map
  2. Brief about Folium
  3. Choropleth maps using Folium
  4. Brief about plotly
  5. Choropleth maps using plotly
  6. Few limitations of choropleth maps

1. What is a Choropleth map

Choropleth is a map or display that divides different geographical regions based on a statistical variable or data variable. It is a type of thematic map where different regions are shaded according to the variable in consideration and the proportion of representation of the variable for a region.

For eg: consider Fig 1 below which shows the Per capita GDP across different countries:

Fig 1: Per capita GDP: Image by author

The map uses colour gradation with respect to GDP per capita to denote the different countries in the map above.

Hence, 2 important things that we will require for creating the choropleth maps is:

a) Geo-spatial data with the geographic boundaries like geo json files and

b) Variables or data points for the colour coding

2. Brief about Folium

Folium is a library in Python that can be used to visualize geo-spatial data.

Leaflet.js is a java script library for creating maps and Folium combines the functionalities of Leaflet with python’s data wrangling abilities (i.e. cleaning and transforming complex datasets into simple formats) in order to create beautiful interactive maps.

Folium helps to bind the data to create maps like choropleth and also enables marker features with HTML visualizations.

Different map projections are also available like orthographic, natural earth etc and different map tiles like MapBox, StamenToner among others.

Now let us consider the global COVID 19 dataset to create a choropleth map.You can get the dataset link in the References.

3. Choropleth maps using Folium

a) First we need to generate the base map. Following code can be used for this:

m = folium.Map(tiles = 'Stamen Terrain',min_zoom = 1.5)
display(m)

We will get an output like this:

Fig 2: Base map by Author

b) We have to get the geodata from the this link and then generate the choropleth map layer as given below:

url = 'https://raw.githubusercontent.com/python-visualization/folium/master/examples/data'
country_shapes = f'{url}/world-countries.json'folium.Choropleth(
geo_data = country_shapes,
min_zoom=2,
name='Covid-19',
data=df,
columns=['Country', 'Confirmed'],
key_on='feature.properties.name',
fill_color='OrRd',
nan_fill_color='black',
legend_name = 'Total Confirmed COVID cases',
).add_to(m)
m

c) Now we will add markers with details about the confirmed cases for each country with this code. The markers can be customized, we are using circular markers here:

def plotDot(point):
folium.CircleMarker(location = (point.latitude, point.longitude),
radius = 5,
weight = 2,
popup = [point.Country, point.Confirmed, point.Recovered],
fill_color = '#000000').add_to(m)covid_df.apply(plotDot, axis = 1)
m.fit_bounds(m.get_bounds())
m

This is the final output with markers of Confirmed and Recovered cases.We can hover around the markers to look at the number of confirmed and recovered cases by country. In the plot below, the marker for India is highlighted:

Fig 3: Folium Choropleth Map with markers: Image by Author

Now, we can create a similar map using plotly.

4. Brief about plotly

Plotly is an open source Python visualization library.It can also be used to create html visualization with great animations. As with Folium, the graphs are interactive and comes with animation features. It is built on top of plotly.js and the visualization can be saved as an image as well.

It also comes with a lot of customization and styling options with implementation being very simple. It is also compatible with dash which makes it easy to embed on blogs or other web applications.

Dash is an open-source framework for building analytical applications, with no Java script required, and it is tightly integrated with the Plotly graphing library. Read more about it here.

With more observations, plotly can give rise to performance issues. The recommendation is to use it offline.

5. Choropleth maps using Plotly

Let us now construct the choropleth map using plotly. This can be done by using a simple code with the same dataset as given below:

fig = px.choropleth(df , locations = 'Country', locationmode = 'country names', color = 'Confirmed'
,animation_frame = 'Date')
fig.update_layout(title_text = 'Global spread of Covid19')
fig.show()

The output is given below:

Fig 4: Plotly Choropleth map(Image by Author)

Here we can have the animation pane by date that will provide the a view of how the confirmed cases has spread across different countries over the months and the rate of increase in the cases.

Animated Choropleth Graph:Global spread of Corona virus over months (By Author)

The HTML visualization of the map can be saved using a single line of code and you can access the HTML version of the map here.

import plotly.offline as py# html file
py.offline.plot(fig, filename='C:/plotlyplots/Global_covid.html')

Summary:

We learnt today how to use two different python libraries to create choropleth maps with the COVID-19 data. Plotly really scores in the area with easy zoom, filter and animation options in maps. However, there is no option of adding customized markers.

Folium is especially helpful with choropleth and other maps as well. The customization options like custom pop ups, custom tiling, markers and other features make Folium a viable contender.Also, Folium is especially built for visualizing geo-spatial data while Plotly has a much wider usage.

Finally few words on Choropleth maps:

Choropleth maps are good for an overall view but some variations within a country/region might get missed because it assumes whole area has same value. There are few other limitations as well, like :

a) Usage of absolute counts can be misleading at times, it is better to use standardized units of measurements like proportions/ratios

b) Boundaries might not be continuous and large areas can dominate the display.

c) There can be concerns in accuracy due to classification issues regarding how the data is classified. Unclassed choropleth maps with graphical values can eliminate the loss of information from classification.

Despite these limitations, choropleth maps can be a very useful method for performing empirical studies and geo-spatial analysis.

Do reach out to me in case you have any questions and drop in your comments.

The full code can be accessed here on Github.

Upvote


user
Created by

Mythili Krishnan


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles