Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Animated choropleth map with discrete colors using Python plotly

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Animated Choropleth Maps can be implemented by Python Plotly. This map is can be composed of colored polygons. We can easily represent spatial variations of any quantity with the help of choropleth maps. To make choropleth maps some basic inputs are required

  1. Geometrical/regional Information: This type of information can be supplied by the GeoJSON file in which each feature has a primary key that uniquely identifies that feature. It can be an id or an identifying value in properties. Also, this value can be given by build-in geometries within python Plotly. 
  2. A list of values indexed with feature identifiers.  

Animated Choropleth India map using Plotly

Python Plotly doesn’t have any in-build dataset that can be used to implement the India maps. So, for plotting India we will use GeoJSON files for Indian municipal boundaries and world region boundaries for Indian cities. We will use some Python libraries like pandas, Plotly, requests, etc. to plot the map.


# importing python libraries
import requests
import as px
import pandas as pd

We can directly use GeoJSON files through the Python request module. First, we use the India district boundary, and then from the boundary of the whole world, we will select only the India portion from that file and extract its cities. 


# extracting india municipal boundaries
indiaMucp= requests.get(
# extracting some cities in india
indiaCity = (
          # getting worlds continental dataset
        # selecting India from world map
        lambda d: d["properties.CNTRY_NAME"].eq("India"), 
        ["properties.CITY_NAME", "geometry.coordinates"],
    .assign( # assigning latitude and longitude of Indian region
        lon=lambda d: d["geometry.coordinates"
                       ].apply(lambda v: v[0]),
        lat=lambda d: d["geometry.coordinates"
                       ].apply(lambda v: v[1]),

Finally, we have all the required datasets and they will be plotted using Plotly. 


# Plotting Indian map and adding layer to view the cities
px.scatter_mapbox(indiaCity, lat="lat", lon="lon",
        "style": "carto-positron",
        "zoom": 4.0,
        "layers": [
                "source": indiaMucp.json(),
                "type": "line",
                "color": "green",
                "line": {"width": 1},


India map

Animated choropleth World map

Firstly we will import Plotly and select the gapmider() dataset which gives a world map continental-wise.


# importing plotly library
import as px
# retrieving dataset
worldMap =

Next, we will plot it to the map which shows the whole world map continental-wise with discrete color style.




From the output, we can see that it shows result in discrete color format and when the cursor is placed on any district it shows the description.

Choropleth map for whole-world

My Personal Notes arrow_drop_up
Last Updated : 17 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials