Plotting Interactive Choropleth Maps in Australia
In this article, we will explore how to plot interactive choropleth maps using Python and various libraries such as Matplotlib, Plotly, and Geopandas.
Introduction
Choropleth maps are a type of thematic map where areas on the surface are colored according to some attribute. In our case, we have a DataFrame with sigmoid functions applied to each area in Australia. We want to create an interactive map that allows us to hover over each area and see the corresponding sigmoid value.
Background
Before diving into code, let’s briefly discuss the concepts involved:
- Geometric Data: The geometry column in our DataFrame represents areas on a 2D plane. These areas are not necessarily bounded by straight lines but can be complex polygons.
**Choropleth Maps**: A choropleth map is a type of thematic map where areas on the surface are colored according to some attribute. In this case, the attributes are sigmoid functions applied to each area in Australia.
Libraries Used
For this article, we will use the following libraries:
Geopandas: This library allows us to easily work with geometric data and create choropleth maps.Plotly: We will use Plotly’s interactive capabilities to create an interactive map that allows us to hover over each area.
Installing Libraries
Before we start coding, make sure you have the necessary libraries installed. You can install them using pip:
pip install geopandas plotly matplotlib
Importing Libraries
First, let’s import the necessary libraries in our code. Here is an example of how to do it:
# Required imports
import geopandas as gpd
import plotly.graph_objects as go
import matplotlib.pyplot as plt
from shapely.geometry import Polygon
Creating a Choropleth Map with Matplotlib
Let’s start by creating a choropleth map using Matplotlib. Here is an example of how to do it:
# Create a sample DataFrame
data = {
'id': [1, 2, 3, 4, 5],
'geometry': [
Polygon([(151.41373, -33.46559), (151.41361, -33.4),
(151.41369, -33.4), (151.41373, -33.46559)]),
Polygon([(151.35398, -33.49854), (151.35397, -33.4),
(151.35394, -33.4), (151.35398, -33.49854)]),
Polygon([(151.20460, -33.53298), (151.20456, -33.5),
(151.20454, -33.5), (151.20460, -33.53298)]),
Polygon([(151.36795, -33.43822), (151.36791, -33.4),
(151.36789, -33.4), (151.36795, -33.43822)]),
Polygon([(151.31006, -33.42699), (151.31020, -33.4),
(151.31018, -33.4), (151.31006, -33.42699)])
],
'sigmoid': [0.410899, 0.615718, 0.386825, 0.373488, 0.040321]
}
df = pd.DataFrame(data)
# Create a choropleth map with Matplotlib
fig, ax = plt.subplots(figsize=(10,6))
merged.plot(column='sigmoid', cmap='OrRd', linewidth=0.8, ax=ax, edgecolor='0.8')
ax.axis('off')
# Show the plot
plt.show()
Creating an Interactive Choropleth Map with Plotly
Now, let’s create an interactive choropleth map using Plotly.
# Create a sample DataFrame
data = {
'id': [1, 2, 3, 4, 5],
'geometry': [
Polygon([(151.41373, -33.46559), (151.41361, -33.4),
(151.41369, -33.4), (151.41373, -33.46559)]),
Polygon([(151.35398, -33.49854), (151.35397, -33.4),
(151.35394, -33.4), (151.35398, -33.49854)]),
Polygon([(151.20460, -33.53298), (151.20456, -33.5),
(151.20454, -33.5), (151.20460, -33.53298)]),
Polygon([(151.36795, -33.43822), (151.36791, -33.4),
(151.36789, -33.4), (151.36795, -33.43822)]),
Polygon([(151.31006, -33.42699), (151.31020, -33.4),
(151.31018, -33.4), (151.31006, -33.42699)])
],
'sigmoid': [0.410899, 0.615718, 0.386825, 0.373488, 0.040321]
}
df = pd.DataFrame(data)
# Create a choropleth map with Plotly
fig = go.Figure(data=go.Choropleth(
locations=df['id'],
z = df['sigmoid'], # Data to color
text = df['id'], # Location labels
colorscale='OrRd',
autocolorscale=False,
reversescale=False,
showlegend=True,
))
# Update the layout
fig.update_layout(margin = {'t':0,'l':0,'r':0,'b':0})
# Show the plot
fig.show()
Creating a GeoJSON Data Source
To use Plotly’s choropleth map, we need to create a GeoJSON data source. Here is an example of how to do it:
# Create a sample GeoJSON file
import json
import geopandas as gpd
data = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[151.41373, -33.46559], [151.41361, -33.4],
[151.41369, -33.4], [151.41373, -33.46559]]
},
'properties': {
'id': 1,
'sigmoid': 0.410899
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [[151.35398, -33.49854], [151.35397, -33.4],
[151.35394, -33.4], [151.35398, -33.49854]]
},
'properties': {
'id': 2,
'sigmoid': 0.615718
}
},
# Add more features here...
]
}
with open('map.geojson', 'w') as f:
json.dump(data, f)
Using the GeoJSON Data Source with Plotly
Now that we have our GeoJSON data source, let’s use it to create a choropleth map with Plotly.
# Load the GeoJSON data source
with open('map.geojson') as f:
gdf = gpd.read_file(f)
# Create a choropleth map with Plotly
fig = go.Figure(data=go.Choropleth(
locations=gdf.index,
z = gdf['sigmoid'], # Data to color
text = gdf['id'], # Location labels
colorscale='OrRd',
autocolorscale=False,
reversescale=False,
))
# Update the layout
fig.update_layout(margin = {'t':0,'l':0,'r':0,'b':0})
# Show the plot
fig.show()
Conclusion
In this article, we explored how to create interactive choropleth maps using Python and various libraries such as Matplotlib, Plotly, and Geopandas. We discussed the concepts involved in creating choropleth maps and provided examples of how to use these libraries to create them.
Last modified on 2024-04-11