My Coding >
Programming language >
Python >
Python libraries and packages >
Pygal >
World map with PyGal
World map with PyGalPyGal 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 mapsWatch our video how to plot world map with PyGal: Continents with PygalTo 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.
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
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.
and then render it to svg file
World country map with PyGalto 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
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.
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.
For the best appearance, the main colour was chousen blue, because first default red is not so obvious. The full code is given below:
|
Last 10 artitles
9 popular artitles
|
|||||||
© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit. |