Tuesday, 10 March 2026

Day 50: Cartogram in Python 🌍📊

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:

CountryPopulation (Millions)
India1400
USA331
China1440
Brazil213
Nigeria223

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

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (216) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (28) Azure (9) BI (10) Books (262) Bootcamp (1) C (78) C# (12) C++ (83) Course (86) Coursera (300) Cybersecurity (29) data (4) Data Analysis (27) Data Analytics (20) data management (15) Data Science (320) Data Strucures (16) Deep Learning (131) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (3) flutter (1) FPL (17) Generative AI (65) Git (10) Google (50) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (259) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (13) PHP (20) Projects (32) Python (1263) Python Coding Challenge (1070) Python Mistakes (50) Python Quiz (439) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (46) Udemy (17) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)