Visualizing Covid 19 cases in Senegal
- Get link
- X
- Other Apps
Visualizing Covid 19 cases in Senegal
In this quick tutorial, we will visualize the proportion of covid 19 cases in different senegalese districts around the country using folium and different other libraries . We will create a statistic map and interactive map.
In [6]:
import folium
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import geopandas
%matplotlib inline
COVID rate data
In [4]:
data = pd.read_csv('D:\Studying\PRACTICES\Geoplot and geopandas geopandas.org documentation\lesson 5 beautiful map statistic maps\Interactive maps\covid_19_confirmed_cases_senegal.csv')
data.head()
Out[4]:
| districts | cases | lat | lon | |
|---|---|---|---|---|
| 0 | DAKAR-OUEST | 2218 | 14.739241 | -17.500376 |
| 1 | DAKAR-SUD | 2000 | 14.661041 | -17.439310 |
| 2 | TOUBA | 608 | 14.867642 | -15.917822 |
| 3 | MBOUR | 274 | 14.416536 | -16.987211 |
| 4 | DAKAR-CENTRE | 2189 | 14.713699 | -17.439182 |
In [7]:
# Convert to geodataframe
gdf = geopandas.GeoDataFrame(
data, geometry=geopandas.points_from_xy(data.lon, data.lat))
In [8]:
gdf.head()
Out[8]:
| districts | cases | lat | lon | geometry | |
|---|---|---|---|---|---|
| 0 | DAKAR-OUEST | 2218 | 14.739241 | -17.500376 | POINT (-17.50038 14.73924) |
| 1 | DAKAR-SUD | 2000 | 14.661041 | -17.439310 | POINT (-17.43931 14.66104) |
| 2 | TOUBA | 608 | 14.867642 | -15.917822 | POINT (-15.91782 14.86764) |
| 3 | MBOUR | 274 | 14.416536 | -16.987211 | POINT (-16.98721 14.41654) |
| 4 | DAKAR-CENTRE | 2189 | 14.713699 | -17.439182 | POINT (-17.43918 14.71370) |
Create a static map
Now we have a spatial layer with the geometry information. Let’s create a simple plot based on cases:
In [9]:
# Define which variable to plot
gdf.plot(column="cases")
Out[9]:
<AxesSubplot:>
In [10]:
# Adjust figure size
fig, ax = plt.subplots(1, figsize=(10, 8))
# Adjust colors and add a legend
gdf.plot(ax = ax, column="cases", scheme="quantiles", cmap="Reds", legend=True)
Out[10]:
<AxesSubplot:>
Create an interactive map
Next, we’ll plot an interactive map based on the same data, and usign the folium library, which enables us to create maps based on the JavaScript library leaflet.js.
In [11]:
# Create a Geo-id which is needed by the Folium (it needs to have a unique identifier for each row)
gdf['geoid'] = gdf.index.astype(str)
In [12]:
gdf.head()
Out[12]:
| districts | cases | lat | lon | geometry | geoid | |
|---|---|---|---|---|---|---|
| 0 | DAKAR-OUEST | 2218 | 14.739241 | -17.500376 | POINT (-17.50038 14.73924) | 0 |
| 1 | DAKAR-SUD | 2000 | 14.661041 | -17.439310 | POINT (-17.43931 14.66104) | 1 |
| 2 | TOUBA | 608 | 14.867642 | -15.917822 | POINT (-15.91782 14.86764) | 2 |
| 3 | MBOUR | 274 | 14.416536 | -16.987211 | POINT (-16.98721 14.41654) | 3 |
| 4 | DAKAR-CENTRE | 2189 | 14.713699 | -17.439182 | POINT (-17.43918 14.71370) | 4 |
In [13]:
gdf.dtypes
Out[13]:
districts object cases int64 lat float64 lon float64 geometry geometry geoid object dtype: object
In [14]:
print(gdf.crs)
None
In [15]:
# Create coordinate reference system for our data
gdf.crs = 'epsg:4326'
gdf.crs
Out[15]:
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
In [17]:
# Create a Map instance
m = folium.Map(location=[14.553810,-14.429443], tiles = 'cartodbpositron', zoom_start=8, control_scale=True)
folium.Choropleth(geo_data = gdf,
samadata = gdf,
columns=['geoid','cases'],
key_on='feature.id',
fill_color='RdYlBu',
line_color='white',
line_weight=0,
legend_name= 'Covid 19 rate in Senegal').add_to(m)
m
- Get link
- X
- Other Apps
Comments
Post a Comment