GeeksforGeeks App
Open App
Browser
Continue

# Plotting World Map Using Pygal in Python

Pygal is a Python module that is mainly used to build SVG (Scalar Vector Graphics) graphs and charts. SVG is a vector-based graphics in the XML format that can be edited in any editor. Pygal can create graphs with minimal lines of code that can be easy to understand and write.

We might want to plot the World Map with country wise boundaries and might even want to represent or differentiate countries on a geographical basis or based on some data in our project. Python Library “Pygal” will help us achieve our task. So let’s get started.

### Installation

```pip install pygal_maps_world
```

Syntax:

```worldmap =  pygal.maps.world.World()
```

Now you can plot the graph of the countries by using their country code. Here is the list of the country code.

Example 1: Plotting Countries Based on Data.

## Python3

 `# import pygal library``import` `pygal`` ` `# create a world map``worldmap ``=`  `pygal.maps.world.World()`` ` `# set the title of the map``worldmap.title ``=` `'Countries'`` ` `# adding the countries``worldmap.add(``'Random Data'``, {``        ``'aq'` `: ``10``,``        ``'cd'` `: ``30``,``        ``'de'` `: ``40``,``        ``'eg'` `: ``50``,``        ``'ga'` `: ``45``,``        ``'hk'` `: ``23``,``        ``'in'` `: ``70``,``        ``'jp'` `: ``54``,``        ``'nz'` `: ``41``,``        ``'kz'` `: ``32``,``        ``'us'` `: ``66``})`` ` `# save into the file``worldmap.render_to_file(``'abc.svg'``)`` ` `print``(``"Success"``)`

Output:

Example 2: Plotting Countries with labels.

## Python3

 `# import pygal``import` `pygal`` ` `# import Style class from pygal.style``from` `pygal.style ``import` `Style`` ` `# create a Style object``custom_style ``=` `Style( colors ``=` `(``'#FF0000'` `, ``'#0000FF'` `,``                                ``'#00FF00'` `, ``'#000000'``,``                                ``'#FFD700'``))`` ` `# create a world map,``# Style class is used for using``# the custom colours in the map,``worldmap ``=`  `pygal.maps.world.World(style ``                                   ``=` `custom_style)`` ` `# set the title of the map``worldmap.title ``=` `'Some Countries Starting from Specific Letters'`` ` `# hex code of colours are used``# for every .add() called``worldmap.add(``'"E" Countries'``, ``             ``[``'ec'``, ``'ee'``, ``'eg'``, ``'eh'``,``              ``'er'``, ``'es'``,``'et'``])`` ` `worldmap.add(``'"F" Countries'``, ``             ``[``'fr'``, ``'fi'``])`` ` `worldmap.add(``'"P" Countries'``, ``             ``[``'pa'``, ``'pe'``, ``'pg'``, ``'ph'``, ``'pk'``, ``               ``'pl'``,``'pr'``, ``'ps'``, ``'pt'``, ``'py'``])`` ` `worldmap.add(``'"Z" Countries'``,``             ``[``'zm'``, ``'zw'``])`` ` `worldmap.add (``'"A" Countries'` `, ``              ``[``'ad'``,``'ae'``, ``'af'``, ``'al'``, ``'am'``, ``'ao'``,``               ``'aq'``, ``'ar'``, ``'at'``, ``'au'``, ``'az'``], ``              ``color ``=` `'black'``)`` ` `# save into the file``worldmap.render_to_file(``'abc.svg'``)`` ` `print``(``"Success"``)`

Output

Example 3: Plotting Continents.

## Python3

 `# import pygal library``import` `pygal`` ` `# create a world map``worldmap ``=`  `pygal.maps.world.SupranationalWorld()`` ` `# set the title of map``worldmap.title ``=` `'Continents'`` ` `# adding the continents``worldmap.add(``'Africa'``, [(``'africa'``)])``worldmap.add(``'North america'``, [(``'north_america'``)])``worldmap.add(``'Oceania'``, [(``'oceania'``)])``worldmap.add(``'South america'``, [(``'south_america'``)])``worldmap.add(``'Asia'``, [(``'asia'``)])``worldmap.add(``'Europe'``, [(``'europe'``)])``worldmap.add(``'Antartica'``, [(``'antartica'``)])`` ` `# save into the file``worldmap.render_to_file(``'abc.svg'``)`` ` `print``(``"Success"``)`

Output:

My Personal Notes arrow_drop_up