Skip to content
Related Articles

Related Articles

Plotting World Map Using Pygal in Python
  • Last Updated : 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.

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.
 

Codes Countries 
adAndorra
aeUnited Arab Emirates
afAfghanistan
alAlbania
amArmenia
aoAngola
aqAntarctica
arArgentina
atAustria
auAustralia
azAzerbaijan
baBosnia and Herzegovina
bdBangladesh
beBelgium
bfBurkina Faso
bgBulgaria
bhBahrain
biBurundi
bjBenin
bnBrunei Darussalam
boBolivia, Plurinational State of
brBrazil
btBhutan
bwBotswana
byBelarus
bzBelize
caCanada
cdCongo, the Democratic Republic of the
cfCentral African Republic
cgCongo
chSwitzerland
ciCote d’Ivoire
clChile
cmCameroon
cnChina
coColombia
crCosta Rica
cuCuba
cvCape Verde
cyCyprus
czCzech Republic
deGermany
djDjibouti
dkDenmark
doDominican Republic
dzAlgeria
ecEcuador
eeEstonia
egEgypt
ehWestern Sahara
erEritrea
esSpain
etEthiopia
fiFinland
frFrance
gaGabon
gbUnited Kingdom
geGeorgia
gfFrench Guiana
ghGhana
glGreenland
gmGambia
gnGuinea
gqEquatorial Guinea
grGreece
gtGuatemala
guGuam
gwGuinea-Bissau
gyGuyana
hkHong Kong
hnHonduras
hrCroatia
htHaiti
huHungary
idIndonesia
ieIreland
ilIsrael
inIndia
iqIraq
irIran, Islamic Republic of
isIceland
itItaly
jmJamaica
joJordan
jpJapan
keKenya
kgKyrgyzstan
khCambodia
kpKorea, Democratic People’s Republic of
krKorea, Republic of
kwKuwait
kzKazakhstan
laLao People’s Democratic Republic
lbLebanon
liLiechtenstein
lkSri Lanka
lrLiberia
lsLesotho
ltLithuania
luLuxembourg
lvLatvia
lyLibyan Arab Jamahiriya
maMorocco
mcMonaco
mdMoldova, Republic of
meMontenegro
mgMadagascar
mkMacedonia, the former Yugoslav Republic of
mlMali
mmMyanmar
mnMongolia
moMacao
mrMauritania
mtMalta
muMauritius
mvMaldives
mwMalawi
mxMexico
myMalaysia
mzMozambique
naNamibia
neNiger
ngNigeria
niNicaragua
nlNetherlands
noNorway
npNepal
nzNew Zealand
omOman
paPanama
pePeru
pgPapua New Guinea
phPhilippines
pkPakistan
plPoland
prPuerto Rico
psPalestine, State of
ptPortugal
pyParaguay
reReunion
roRomania
rsSerbia
ruRussian Federation
rwRwanda
saSaudi Arabia
scSeychelles
sdSudan
seSweden
sgSingapore
shSaint Helena, Ascension and Tristan da Cunha
siSlovenia
skSlovakia
slSierra Leone
smSan Marino
snSenegal
soSomalia
srSuriname
stSao Tome and Principe
svEl Salvador
sySyrian Arab Republic
szSwaziland
tdChad
tgTogo
thThailand
tjTajikistan
tlTimor-Leste
tmTurkmenistan
tnTunisia
trTurkey
twTaiwan (Republic of China)
tzTanzania, United Republic of
uaUkraine
ugUganda
usUnited States
uyUruguay
uzUzbekistan
vaHoly See (Vatican City State)
veVenezuela, Bolivarian Republic of
vnViet Nam
yeYemen
ytMayotte
zaSouth Africa
zmZambia
zwZimbabwe

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: 
 

countries plot

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 
 

countries plot

 

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: 
 

continents plot

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :