Day 50: Cartogram in Python 🌍📊
Maps are one of the most powerful ways to visualize geographic data. But sometimes, showing countries by their actual land area does not represent the true importance of the data you want to display.
That’s where a Cartogram comes in.
A Cartogram is a special type of map where the size or appearance of regions changes based on a data variable, such as population, GDP, or election results. Instead of geographic size, the visualization emphasizes data magnitude.
In this example, we create a population cartogram-style visualization using Plotly in Python.
What is a Cartogram?
A Cartogram is a map where geographic regions are rescaled or emphasized according to statistical data.
For example:
-
Population cartograms show countries sized by population
-
Economic cartograms resize regions based on GDP
-
Election cartograms scale areas by votes
The goal is to make data importance visually clear rather than strictly preserving geographic accuracy.
Dataset Used
In this example, we create a simple dataset containing populations of five countries:
| Country | Population (Millions) |
|---|---|
| India | 1400 |
| USA | 331 |
| China | 1440 |
| Brazil | 213 |
| Nigeria | 223 |
The bubble size on the map will represent the population size of each country.
Python Code
import plotly.express as px
import pandas as pd
# Create dataset
df = pd.DataFrame({
"Country": ["India", "USA", "China", "Brazil", "Nigeria"],
"Population": [1400, 331, 1440, 213, 223] # in millions
})
# Create cartogram-style map
fig = px.scatter_geo(
df,
locations="Country",
locationmode="country names",
size="Population",
projection="natural earth",
title="Population Cartogram (Bubble Style)"
)
fig.show()
Code Explanation
1️⃣ Import Libraries
import plotly.express as px
import pandas as pd
-
Pandas is used to create and manage the dataset.
-
Plotly Express is used for interactive geographic visualizations.
2️⃣ Create the Dataset
df = pd.DataFrame({
"Country": ["India", "USA", "China", "Brazil", "Nigeria"],
"Population": [1400, 331, 1440, 213, 223]
})
We create a simple dataset containing:
-
Country names
-
Population values (in millions)
3️⃣ Create the Geographic Visualization
fig = px.scatter_geo(...)
The scatter_geo() function places points on a world map.
Key parameters:
-
locations → Country names used to locate them on the map
-
locationmode → Specifies that locations are country names
-
size → Controls bubble size based on population
-
projection → Determines map style (Natural Earth projection)
4️⃣ Display the Chart
fig.show()
This renders an interactive geographic chart where:
-
Each country appears on the map
-
Bubble size reflects population magnitude
What Insights Can We See?
From this visualization:
-
China and India have the largest bubbles, representing their massive populations.
-
USA appears significantly smaller than the two Asian giants.
-
Brazil and Nigeria show medium-sized population bubbles.
This allows us to quickly compare population sizes geographically.
When Should You Use a Cartogram?
Cartograms are useful when visualizing data related to geography such as:
-
Population distribution
-
Economic indicators
-
Election results
-
Resource usage
-
Disease spread
-
Demographic statistics
They help emphasize data importance rather than land area.
Why Use Plotly?
Plotly makes geographic visualizations powerful because it provides:
-
Interactive charts
-
Zoomable maps
-
Hover tooltips
-
High-quality visuals
This makes it ideal for data science dashboards and presentations.
Conclusion
A Cartogram transforms traditional maps into data-driven visualizations, allowing us to quickly understand the significance of geographic data. In this example, we used Plotly and Python to create a population cartogram where bubble sizes represent the population of different countries.
Even with a small dataset, the visualization clearly highlights how population varies across the world.


0 Comments:
Post a Comment