Data were obtained from the ArcGIS Hub and provide details of the various types of land cover in Malawi.
This is an example of how to use plotly express and choropleth mapbox to plot geospatial data.
# import geojson file from ArcGIS Open Data Hub
from urllib.request import urlopen
import json
with urlopen('https://opendata.arcgis.com/datasets/7533fe68b047461ea6187841c706375b_3.geojson') as response:
malawi = json.load(response)
# print first 10 features
malawi["features"][10]
# import df with same object ID from ArcGIS server
import pandas as pd
df = pd.read_csv("Malawi_2016.csv",
dtype={"NAME": str})
import geopandas as gpd
# load plotly express
import plotly.express as px
# print head of df
df.head(10)
df.info()
# plot histogram of type of landcover
import plotly.graph_objects as go
fig = px.pie(df, values = "OBJECTID", names="NAME", title = "Type of land cover in Malawi (MASDAP 2010)")
fig = go.FigureWidget(fig)
fig
# plot histogram of sum hectare per land use area
fig = px.histogram(df, x="NAME", y="HECTARES").update_xaxes(categoryorder="total descending")
fig.update_layout(
title={
'text': "Sum of hectares per land use area (MASDAP 2010)",
'y':0.95,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'})
fig.update_yaxes(showgrid=False) # turning off the grid
fig = go.FigureWidget(fig)
fig
# plot choropleth mapbox with plotly express using geojson and df data
import plotly.graph_objs as go
fig = px.choropleth_mapbox(df, geojson=malawi, color="HECTARES",
locations="HECTARES", featureidkey="properties.HECTARES",
center={"lat": -13.254308, "lon": 34.301525},
mapbox_style="carto-positron", zoom=9,
labels={'HECTARES':'Hectares'})
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig = go.FigureWidget(fig)
fig
# subset data to only show values for Dambo area/agriculture & Forest
df_subset = df[df.NAME.isin(["Dambo area/agriculture", "Forest", "Agriculture/settlement", "Forest Plantation"])]
print(df_subset.head(10))
# plot choropleth mapbox with plotly express using geojson and df data
fig = px.choropleth_mapbox(df_subset, geojson=malawi, color="NAME",
locations="HECTARES", featureidkey="properties.HECTARES",
center={"lat": -13.254308, "lon": 34.301525},
mapbox_style="carto-positron", zoom=9,
labels={'NAME':'Land cover'})
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig