We have seen parameters used in our pygal-related chapters throughout the book. Here, we will start with our regions-based France chart, but first let's go ahead and fill in the rest of the regions on the chart. Copy the following code into your editor of choice and render the chart:
# -*- coding: utf-8 -*- import pygal france_chart = pygal.FrenchMap_Regions() france_chart.title = 'Sample Regions' france_chart.add('Alsace', ['42']) france_chart.add('Aquitaine', ['72']) france_chart.add('Auvergne', ['83']) france_chart.add('Brittany', ['53']) france_chart.add('Burgundy', ['26']) france_chart.add('Centre', ['24']) france_chart.add('Champagne-Ardenne', ['21']) france_chart.add(unicode('Franche-Comté', 'utf-8'), ['43']) france_chart.add(unicode('Île-de-France', 'utf-8'), ['11']) france_chart.add('Languedoc-Roussillon', ['91']) france_chart.add('Limousin', ['74']) france_chart.add('Lorraine', ['41']) france_chart.add('Lower Normandy', ['25']) france_chart.add(unicode('Midi-Pyrénées', 'utf-8'), ['73']) france_chart.add('Nord-Pas-de-Calais', ['31']) france_chart.add('Pays de la Loire', ['52']) france_chart.add('Picardy', ['22']) france_chart.add('Poitou-Charentes', ['54']) france_chart.add(unicode('Provence-Alpes-Côte d'Azur', 'utf-8'), ['93']) france_chart.add(unicode('Rhône-Alpes', 'utf-8'), ['83']) france_chart.add('Upper Normandy', ['23']) france_chart.add('Corsica', ['94']) france_chart.add('French Guiana', ['03']) france_chart.add('Guadeloupe', ['01']) france_chart.add('Mayotte', ['05']) france_chart.add('Reunion', ['04']) france_chart.render_to_file('france_map.svg')
When done, you should see what is shown in the following screenshot:
In the preceding screenshot, we can see quite a few regions in our legend. Notice how much space they take up in our chart. Let's tinker with some formatting parameters to clean this up.
We can reposition the legend for the chart using the legend_at_bottom
parameter and passing in a value either as True
or False
. Here's an example and the output screen. Notice the legend_at_bottom
parameter in the FrenchMap_Regions()
method:
# -*- coding: utf-8 -*- import pygal france_chart = pygal.FrenchMap_Regions(legend_at_bottom=True) france_chart.title = 'Sample Regions' france_chart.add('Alsace', ['42']) france_chart.add('Aquitaine', ['72']) france_chart.add('Auvergne', ['83']) france_chart.add('Brittany', ['53']) france_chart.add('Burgundy', ['26']) france_chart.add('Centre', ['24']) france_chart.add('Champagne-Ardenne', ['21']) france_chart.add(unicode('Franche-Comté', 'utf-8'), ['43']) france_chart.add(unicode('Île-de-France', 'utf-8'), ['11']) france_chart.add('Languedoc-Roussillon', ['91']) france_chart.add('Limousin', ['74']) france_chart.add('Lorraine', ['41']) france_chart.add('Lower Normandy', ['25']) france_chart.add(unicode('Midi-Pyrénées', 'utf-8'), ['73']) france_chart.add('Nord-Pas-de-Calais', ['31']) france_chart.add('Pays de la Loire', ['52']) france_chart.add('Picardy', ['22']) france_chart.add('Poitou-Charentes', ['54']) france_chart.add(unicode('Provence-Alpes-Côte d'Azur', 'utf-8'), ['93']) france_chart.add(unicode('Rhône-Alpes', 'utf-8'), ['83']) france_chart.add('Upper Normandy', ['23']) france_chart.add('Corsica', ['94']) france_chart.add('French Guiana', ['03']) france_chart.add('Guadeloupe', ['01']) france_chart.add('Mayotte', ['05']) france_chart.add('Reunion', ['04']) france_chart.render_to_file('france_map.svg')
We can also format the legend box using the legend_box_size
parameter which allows for an integer value; this will change the colored box sizes for each legend item. Here is an example:
# -*- coding: utf-8 -*- import pygal france_chart = pygal.FrenchMap_Regions(legend_at_bottom=True, legend_box_size=3) france_chart.title = 'Sample Regions' france_chart.add('Alsace', ['42']) france_chart.add('Aquitaine', ['72']) france_chart.add('Auvergne', ['83']) france_chart.add('Brittany', ['53']) france_chart.add('Burgundy', ['26']) france_chart.add('Centre', ['24']) france_chart.add('Champagne-Ardenne', ['21']) france_chart.add(unicode('Franche-Comté', 'utf-8'), ['43']) france_chart.add(unicode('Île-de-France', 'utf-8'), ['11']) france_chart.add('Languedoc-Roussillon', ['91']) france_chart.add('Limousin', ['74']) france_chart.add('Lorraine', ['41']) france_chart.add('Lower Normandy', ['25']) france_chart.add(unicode('Midi-Pyrénées', 'utf-8'), ['73']) france_chart.add('Nord-Pas-de-Calais', ['31']) france_chart.add('Pays de la Loire', ['52']) france_chart.add('Picardy', ['22']) france_chart.add('Poitou-Charentes', ['54']) france_chart.add(unicode('Provence-Alpes-Côte d'Azur', 'utf-8'), ['93']) france_chart.add(unicode('Rhône-Alpes', 'utf-8'), ['83']) france_chart.add('Upper Normandy', ['23']) france_chart.add('Corsica', ['94']) france_chart.add('French Guiana', ['03']) france_chart.add('Guadeloupe', ['01']) france_chart.add('Mayotte', ['05']) france_chart.add('Reunion', ['04']) france_chart.render_to_file('france_map.svg')
The following screenshot shows the results of our script:
Check out how we used both the legend_at_bottom
and legend_box_size
parameters. We can stack them using a comma between parameters and allow many combinations. We still have an excess of labels bleeding into the next columns of the legend; let's go ahead and tweak them with these other properties.
For the next few code samples, I'll trim out the datasets so that we can focus on understanding parameters. If you need to regrab, refer to our initial chart from the Parameters section. Now, let's shrink down the font size of our labels and resize our boxes with the legend_font_size
parameter:
# -*- coding: utf-8 -*- import pygal france_chart = pygal.FrenchMap_Regions(legend_at_bottom=True, legend_box_size=8, legend_font_size=8) # ///Data-sets (continued)
The following screenshot shows the results of the script:
Nice! Now, one legend item is still a bit small; let's trim it along with the others for consistency, using the truncate_legend
parameter, as shown in the following code:
# -*- coding: utf-8 -*- import pygal france_chart = pygal.FrenchMap_Regions(legend_at_bottom=True, legend_box_size=8, legend_font_size=8, truncate_legend=6)
The following screenshot shows the results of our script:
Well done! This is now looking more and more organized. We can even disable the legend_at_bottom
parameter by either removing or setting it to False
. It will look something like what is shown in the following screenshot:
One more thing to note about legends is that you can disable them if you don't need them. Keep in mind that chart legends help users of the charts to help consume the data. Since we don't have an x and y axes labels, we should be fine to disable the legend. To do this, you just need to set the show_legend
parameter to False
, as shown in the following code:
# -*- coding: utf-8 -*- import pygal france_chart = pygal.FrenchMap_Regions(show_legend=False, legend_box_size=8, legend_font_size=8, truncate_legend=6)
The following screenshot shows the results of our script, without a legend:
Excellent! Now, we've modified the legend to allow greater flexibility for our charts.
18.220.53.93