Choropleth Map in Plotly
Last Updated: 08 Nov 2025
Choropleth maps use color intensity to show data by region — perfect for population density, election results, sales by state, COVID cases, or GDP per country.
Hinglish Tip: Choropleth = "jahan zyada data, wahan dark color" — jaise India ka weather map! Hover karo → exact number dikhega.
Basic Choropleth Map (World)
import plotly.express as px
# Built-in dataset
df = px.data.gapminder().query("year == 2007")
fig = px.choropleth(
df,
locations="iso_alpha", # 3-letter country code
color="gdpPercap",
hover_name="country",
color_continuous_scale="Viridis",
title="World GDP per Capita (2007)"
)
fig.show()
Key Parameters in px.choropleth()
Full Example: India State-wise Literacy Rate
import plotly.express as px
import pandas as pd
# Sample India data (use real data in practice)
india_data = {
'State': ['Maharashtra', 'Kerala', 'Tamil Nadu', 'UP', 'Bihar', 'Karnataka', 'Gujarat'],
'Literacy (%)': [82.3, 94.0, 80.1, 67.7, 61.8, 75.4, 78.0],
'Population (Cr)': [11.2, 3.3, 7.2, 19.9, 10.4, 6.1, 6.0],
'State Code': ['MH', 'KL', 'TN', 'UP', 'BR', 'KA', 'GJ']
}
df = pd.DataFrame(india_data)
fig = px.choropleth(
df,
geojson="https://github.com/geohacker/india/blob/master/state/india_state.geojson",
featureidkey="properties.ST_CODE", # Match with geojson
locations="State Code",
color="Literacy (%)",
hover_name="State",
hover_data=["Population (Cr)"],
color_continuous_scale="Oranges",
title="India: State-wise Literacy Rate (%)",
scope="asia",
labels={'Literacy (%)': 'Literacy Rate'}
)
fig.update_geos(
fitbounds="locations",
visible=False
)
fig.update_layout(
coloraxis_colorbar=dict(
title="Literacy %",
tickvals=[60, 70, 80, 90],
ticktext=['Low', 'Medium', 'High', 'Very High']
),
font=dict(size=12)
)
fig.show()
Hinglish Tip: fitbounds="locations" → sirf India dikhega, no extra world! Get GeoJSON for India: github.com/geohacker/india
USA State Map (Built-in)
df = px.data.election()
fig = px.choropleth(
df,
locations="district", # FIPS codes
locationmode="USA-states",
color="Bergeron",
scope="usa",
title="Canada Election Results (Sample)"
)
fig.show()
Animated Map (Over Time)
df = px.data.gapminder()
fig = px.choropleth(
df,
locations="iso_alpha",
color="lifeExp",
hover_name="country",
animation_frame="year",
color_continuous_scale="Plasma",
title="Life Expectancy Over Time (1952-2007)"
)
fig.show()
Hinglish Tip: animation_frame="year" → time travel map — dekho duniya kaise badli!
