Plotting World Map Using Pygal in Python

With the help of the Pygal library of Python, we can create stunning world maps in Python as it provides different functions to create and customize the graphs. This article explores the step-by-step process of plotting a world map, customizing the map's style, adding data to highlight countries or regions, and rendering the map to an SVG file. Whether you want to visualize geographic data, showcase international statistics, or create interactive visualizations, Pygal provides a powerful toolset for displaying global information with ease.

Prerequisites

Before we start, make sure you have Pygal installed ?

pip install pygal

Plotting Countries on the World Map

Below are the steps that we will follow to plot countries on the world map ?

  • We import the pygal.maps.world module to create a world map. We create an instance of the World class to represent the map.

  • We set the title of the map to 'Countries of the World' using the title attribute.

  • We add data to the map using the add() method. In the below example, we provide a list of tuples where each tuple represents a country and its associated data. The first element of each tuple is the two-letter country code (e.g., 'ca' for Canada, 'us' for the United States, and 'mx' for Mexico), and the second element is the data value (here, all countries have a value of 1).

  • Finally, we render the map to an SVG file using the render_to_file() method, specifying the desired filename ('countries_map.svg' in this case).

Example

import pygal.maps.world

# Create a world map object
worldmap = pygal.maps.world.World()

# Set the title of the map
worldmap.title = 'Countries of the World'

# Add data to the map
worldmap.add('Countries', [('ca', 1), ('us', 1), ('mx', 1)])  # Canada, USA, Mexico

# Render the map to an SVG file
worldmap.render_to_file('countries_map.svg')
print("World map with countries saved as 'countries_map.svg'")
World map with countries saved as 'countries_map.svg'

While running the program, it will generate an SVG file that represents the world map with the specified title and the countries Canada, USA, and Mexico highlighted on the map.

Plotting Continents on the World Map

Follow the steps given below to plot continents in the world map ?

  • Import the necessary module ? The program example given below starts by importing the pygal.maps.world module, which provides the functionality to create world maps.

  • Create a world map object ? An instance of the World class is created using pygal.maps.world.World(). This object represents the world map that will be plotted.

  • Set the title ? The title attribute of the world map object is set to 'Continents'. This will be the title displayed on the map.

  • Define a dictionary mapping countries to continents ? The program below defines a dictionary country_to_continent, where each country's two-letter code is mapped to its corresponding continent.

  • Plot the continents by adding countries ? The program below iterates over the unique continents present in the dictionary. For each continent, it extracts the countries belonging to that continent and adds them to the world map using the add() method.

  • Render the map to an SVG file ? Finally, the world map is rendered to an SVG file named 'continents_map.svg' using the render_to_file() method.

Example

import pygal.maps.world

# Create a world map
worldmap = pygal.maps.world.World()

# Set the title
worldmap.title = 'Continents'

# Define a dictionary mapping countries to continents
country_to_continent = {
    'af': 'africa',
    'cn': 'asia',
    'in': 'asia',
    'us': 'north_america',
    'ca': 'north_america',
    'br': 'south_america',
    'ar': 'south_america',
    'fr': 'europe',
    'de': 'europe',
    'au': 'oceania'
}

# Plot the continents by adding countries belonging to each continent
for continent in set(country_to_continent.values()):
    countries = [country for country, cont in country_to_continent.items() if cont == continent]
    worldmap.add(continent.title(), countries)

# Render the map to an SVG file
worldmap.render_to_file('continents_map.svg')
print("Continental map saved as 'continents_map.svg'")
Continental map saved as 'continents_map.svg'

Plotting World Map with Custom Styling and Data

Below are the steps that we will follow to plot the world map with custom styling and data values ?

  • Create a world map object ? The program starts by creating an instance of the World class from the pygal.maps.world module.

  • Set the title ? The program sets the title attribute of the world map object to 'World Population Data'.

  • Customize the style of the map ? The program customizes the style of the map by assigning the LightColorizedStyle from the pygal.style module to provide a light color scheme to the map.

  • Add data to the map ? The program uses the add() method to add data to the map. The data is provided as a dictionary where keys are two-letter country codes and values represent the associated data for each country.

  • Render the map to an SVG file ? Finally, the world map is rendered to an SVG file using the render_to_file() method.

Example

import pygal.maps.world
import pygal.style

# Create a world map
worldmap = pygal.maps.world.World()

# Set the title
worldmap.title = 'World Population Data (in millions)'

# Customize the style of the map
worldmap.style = pygal.style.LightColorizedStyle

# Add population data to the map
worldmap.add('Population', {
    'cn': 1439,  # China
    'in': 1380,  # India
    'us': 331,   # United States
    'id': 273,   # Indonesia
    'br': 213,   # Brazil
    'pk': 220,   # Pakistan
    'bd': 165,   # Bangladesh
    'ng': 206,   # Nigeria
    'ru': 146,   # Russia
    'mx': 129    # Mexico
})

# Render the map to an SVG file
worldmap.render_to_file('world_population_map.svg')
print("World population map saved as 'world_population_map.svg'")
World population map saved as 'world_population_map.svg'

Common Country Codes

Here are some commonly used two-letter country codes for mapping ?

Country Code Country Name Country Code Country Name
us United States cn China
in India br Brazil
ca Canada au Australia
fr France de Germany
jp Japan ru Russia

Key Features

  • Easy SVG Output ? Pygal generates scalable vector graphics that can be embedded in web pages

  • Interactive Maps ? The generated maps are interactive by default with hover effects

  • Customizable Styles ? Multiple built-in styles and the ability to create custom themes

  • Data Visualization ? Perfect for displaying geographical statistics and country comparisons

Conclusion

Pygal proves to be a powerful and user-friendly tool for plotting world maps in Python. With its intuitive syntax and extensive customization options, we can effortlessly create visually stunning representations of global data. The library's ability to generate interactive SVG maps makes it ideal for web-based data visualization projects.

Updated on: 2026-03-27T09:46:29+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements