Plotting World Map Using Pygal in Python

28 Jul, 2020
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.
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 libraryimport pygal  # create a world mapworldmap =  pygal.maps.world.World()  # set the title of the mapworldmap.title = 'Countries'  # adding the countriesworldmap.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 fileworldmap.render_to_file('abc.svg')  print("Success")
Output:  
Example 2: Plotting Countries with labels.
Python3
# import pygalimport pygal  # import Style class from pygal.stylefrom pygal.style import Style  # create a Style objectcustom_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 mapworldmap.title = 'Some Countries Starting from Specific Letters'  # hex code of colours are used# for every .add() calledworldmap.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 fileworldmap.render_to_file('abc.svg')  print("Success")
Output  
 
Example 3: Plotting Continents.
Python3
# import pygal libraryimport pygal  # create a world mapworldmap =  pygal.maps.world.SupranationalWorld()  # set the title of mapworldmap.title = 'Continents'  # adding the continentsworldmap.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 fileworldmap.render_to_file('abc.svg')  print("Success")
Output:  
Data Visualization
Python-pygal
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.