Lei Duan
Lei Duan

Reputation: 33

How to set specific color for each category when using "pygal.maps.world" drawing world map?

Does anyone use pygal.maps.world in pygal to draw a world map? I have 5 different categories that includes several countries for each category. How can I set specific color for each category?

Below are part of my code:

supra = pygal.maps.world.SupranationalWorld()
supra.add('Asia', Asia_list)
supra.add('Europe', Europe_list)
supra.add('Africa', Africa_list)
supra.render()

Asia_list, Europe_list, and Africa_list are three lists I defined that include certain countries.

How can I set color by myself? Thanks!

Upvotes: 2

Views: 1787

Answers (1)

Alan H
Alan H

Reputation: 3111

First, you can override the default colors that pygal will use with the Style class. The format I used below is by assigning a hex triplet value for the first three colors pygal will use. To add more colors, just add more to the list. For more info on a hex triplet, this wikipedia page can help: https://en.wikipedia.org/wiki/Web_colors

Here are the Style changes:

from pygal.style import Style
custom_style = Style(colors=('#FFFF00', '#00FFFF', '#FF00FF'))

Second, I was not able to do this style change with the SupranationalWorld like your example above, but I was able to just use the following which I believe does what you requested:

worldmap_chart = pygal.maps.world.World(style=custom_style)
worldmap_chart.add('Asia', Asia_list)
worldmap_chart.add('Europe', Europe_list)
worldmap_chart.add('Africa', Africa_list)
worldmap_chart.render_to_file('world.svg')

The documentation for all the things you can customize with style can be found here: http://www.pygal.org/en/stable/documentation/custom_styles.html

Upvotes: 1

Related Questions