World map with PyGal
PyGal can plot world map in format of SVG, or convert SVG to plain graphical image. This format is very convenient id you need to add some interactions to the map. For example, add links to countries as a reference, which is very convenient for HTML usage. Therefore PyGal can be very useful for building web-applications.
Unfortunately, there is no clear mechanism, how to build your own map with PyGal and you only really limited with present world map with continents, countries and detailed maps for France and Swiss.
How to use PyGal for maps
Watch our video how to plot world map with PyGal:
Continents with Pygal
To plot continents, you need to use name of continent from the given list: asia, europe, africa, north_america, south_america, oceania and antartica.
First of all, you need to include the following libraries: pygal_maps_world for world map, pygal.style for changing style and cairosvg for converting to the plain image if necessary.
from pygal_maps_world.maps import SupranationalWorld as SW
from pygal.style import Style
Then you need to define colour for the first few elements (can do for as many as you like), or if you skip it – colours will be generated automatically. Colors should be given in format #RRGGBB or #RRGGBBAA
my_style = Style(colors=('#FF000077',)) # dond forget about coma if use only one colour
wm = SW(style=my_style)
Now you can add objects with with any selection of elements. All elements of the same object will have the same colour but with different relative intensity, according to given value.
wm.add('Oceania',[('oceania',6)])
wm.add('Africa',[{‘value':('africa',10),
'xlink': 'http://africa.com/',}])
wm.add('America',[('north_america', 10),('south_america', 20)])
and then render it to svg file
wm.render_to_file('wm.svg')
World country map with PyGal
to plot countries, you need to know country lists first. In this example, I will use CSV file with information about population density in North America, given in format Code,Country,Density,Reference. To read CSV file, I will use Panda library.
For these examples I've got 104_density.csv file, taken from Wikipedia
North America country data 104_density.csv (936 b) |
Information about North American countries, Country code, Name, Population density and URL links to the Wikipedia pages about these countries. |
import pandas as pd
from pygal.maps.world import World
df = pd.read_csv('density.csv', sep=',')
Then I will plot every country with its own colour. This will be pretty useless map, but it is ok for the first attempt. Each country is generated as it is own object.
wm = World()
wm.title = 'North America Population Density'
[wm.add(n, {c:d}) for n, c, d in zip(df.Name, df.Code, df.Density)]
wm.render_to_file('./wm.svg')
North America Population Density (size: 827,943 b) |
In this demo, I will create only one group North America and colour each country into default colour with the intensity, proportional to the density. Also I will make a link to wikipedia for each country.
wm = World()
wm.title = 'North America Population Density'
wm.add('North America', [(
{'value':(c,d),'xlink':r}) for c,d,r in zip(df.Code,
df.Density,
df.Reference)])
North America Population Density with country colour intensity, proportional to the population density in the country. (size: 823,640 b) |
For the best appearance, the main colour was chousen blue, because first default red is not so obvious.
The full code is given below:
import pandas as pd
from pygal.maps.world import World
from pygal.style import Style
df = pd.read_csv('density.csv', sep = ',')
my_style = Style(colors=('#0000FF',)) # dond forget about coma if use only one colour
wm = World(style=my_style)
wm.title = 'North America Population Density'
wm.add('North America', [(
{'value':(c,d),'xlink':r}) for c,d,r in zip(df.Code,
df.Density,
df.Reference)])
wm.render_to_file('104_NA_density_colour.svg')
Published: 2022-08-01 02:45:38
Updated: 2022-08-01 15:02:38