I don't think the worldmap charts need much introduction. It's a worldmap that is separated into countries, which outputs to an SVG file. The worldmap chart is a fantastic feature of the pygal
library and is partially why I like this Python charting library. This is because not many Python charting libraries have maps as a feature, let alone ones that use the SVG output, making the map charts pygal produce very portable chart files, in our modern HTML5 mobile world we live in today.
A simple worldmap is easy to build with a little bit of dummy data using the pygal library. Let's build a simple worldmap with only United States
and China
highlighted as an example. Copy the following code into your editor of choice, run your Python script, and let's take a look at the results. Also, ensure that you save your output SVG file as world_map.svg
.
# -*- coding: utf-8 -*- import pygal worldmap_chart = pygal.Worldmap() worldmap_chart.title = 'Highlighting China and the United States' worldmap_chart.add('China', ['cn']) worldmap_chart.add('United States', ['us']) #Render file. worldmap_chart.render_to_file('world_map.svg')
Open the world_map.svg
file, and the result will be as shown in the following screenshot:
So, in the preceding screenshot, we have our output worldmap, and we can verify that both China
and United States
are highlighted. We can also see Hawaii and Alaska properly highlighted in United States
. Let's review our code and see what's different with the worldmap in comparison with our other charts.
Take a look at our add()
functions for our world_map
variable, and take a look at the two parameters passed in it, as shown in the following code:
worldmap_chart.add('China', ['cn']) worldmap_chart.add('United States', ['us'])
Notice that our add()
function works similar to our past chart; however, this time, we are passing in a string instead of a number inside an array, in this case, a single item array with a string. This string is actually a two-letter country code, and since it's a standard country code pygal can set values to a specific country on our map.
Going back to the array in our add()
method, what happens if we add multiple countries to an array for a single add()
function? Let's rework our chart in order to allow multiple countries to be highlighted.
This time, we will rename United States
and change the label to U.S. Allies
. Let's add these allies along with our us
country code and see what happens. Also, I'll break our array up to a new line for each country code (each country code is taken to a new line in the code bundle ) so that we can easily read or if we need to update our code:
# -*- coding: utf-8 -*- import pygal worldmap_chart = pygal.Worldmap() worldmap_chart.title = 'United States Allies and China' worldmap_chart.add('China', ['cn']) worldmap_chart.add('U.S. Allies', ['al','be','bg','ca','hr','cz','dk','ee','ff','de','hu','is','it','lv','lt','lu','nl','no','pl','pt','ro','si','sk','tr','us','uk']) #Render file. worldmap_chart.render_to_file('world_map.svg')
Open the world_map.svg
file, and the result will be as shown in the following screenshot:
Not bad; we can easily see the chart highlighting our U.S. Allies
with the same color, while China
is highlighted with another color on a separate dataset.
Worldmaps are great SVG charts. One thing to note is that worldmaps are very complex SVG images, so consider the amount of data your own charts will include, and steer clear of extremely complex datasets. Some mobile platforms that render SVGs with animations might turn out sluggish when deployed.
18.217.150.123