In this chapter, we will cover how to apply themes and use some of the optional features used in the pygal
library. We will also look at themes and styling of our charts.
The pygal
library has a chart that we didn't review in the past chapters; this is a good time to bring the topic up and discuss the chart. It's called the country chart. Like the worldmap chart, it shows the deeper detailed regions of a country; unfortunately, at the time of writing this book, it's limited only to the country of France, and it works pretty similar to the world map.
If you remember our introductory chapter, you might remember our discussion on Kozea, the open source community originating in France that developed the pygal
library. Since they came from France, they created a map for their own country, including sectioned areas called departments and regions. France's regions are similar to states, and departments are similar to counties in a state.
Let's take a look at some sample code for this map, and we can then build on it using some extra features of pygal's frameworks. First, let's build a simple example using departments. Create a new Python file and copy the following code into your editor of choice. Notice the similarities between this and our worldmap code in Chapter 4, Advanced Charts. Be sure to use france_map.svg
as the file output:
# -*- coding: utf-8 -*- import pygal france_chart = pygal.FrenchMap_Departments() france_chart.title = 'Sample departments' france_chart.add('Data-set 1', ['17']) france_chart.add('Data-set 2', ['27']) france_chart.add('Data-set 3', ['38']) france_chart.add('Data-set 4', ['42']) france_chart.add('Data-set 5', ['19']) france_chart.render_to_file('france_map.svg')
Open the france_map.svg
file, and the result will be what's shown in the following screenshot:
Let's take a look at our france_chart
type. Note that we used pygal.FrenchMap_Departments()
with our chart type suffixed with Departments
. With this chart, there are two modes—one for departments and the other for regions. Use the following code to see how region-based charts are created. Note the suffix this time:
# -*- coding: utf-8 -*- import pygal france_chart = pygal.FrenchMap_Regions() france_chart.title = 'Sample Regions' france_chart.add('Centre', ['24']) france_chart.add('Lorraine', ['41']) france_chart.add('Picardy', ['22']) france_chart.add('Upper Normandy', ['23']) france_chart.add('Corsica', ['94']) france_chart.render_to_file('france_map.svg')
Open the france_map.svg
file, and the result will be what's shown in the following screenshot:
Looking closer at the code, we might wonder why we aren't using abbreviations for regions, or ask how to set our active regions or departments. The reason for the numbers is that France uses its own INSEE numbers.
Institut National de la Statistique et des Études Économiques (INSEE) is France's national institute for statistics and economics, which created the numbering system for its departments and regions. Since this is a common system to identify sections of France, the pygal
library developers use the same numbers to assign highlights to the map chart. Now that we know how to use this chart, let's modify it this time, not with data, but with parameters, methods, and themes included in the pygal
library.
3.16.51.246