在Python中使用 Pygal 绘制世界地图
Pygal是一个Python模块,主要用于构建 SVG(Scalar Vector Graphics)图形和图表。 SVG 是 XML 格式的基于矢量的图形,可以在任何编辑器中进行编辑。 Pygal 可以用最少的代码行来创建易于理解和编写的图形。
我们可能想要绘制具有国家边界的世界地图,甚至可能想要在地理基础上或基于我们项目中的某些数据来表示或区分国家。 Python库“Pygal”将帮助我们完成任务。所以让我们开始吧。
安装
pip install pygal_maps_world
句法:
worldmap = pygal.maps.world.World()
现在您可以使用国家/地区代码绘制国家/地区的图表。这是国家代码的列表。Codes Countries ad Andorra ae United Arab Emirates af Afghanistan al Albania am Armenia ao Angola aq Antarctica ar Argentina at Austria au Australia az Azerbaijan ba Bosnia and Herzegovina bd Bangladesh be Belgium bf Burkina Faso bg Bulgaria bh Bahrain bi Burundi bj Benin bn Brunei Darussalam bo Bolivia, Plurinational State of br Brazil bt Bhutan bw Botswana by Belarus bz Belize ca Canada cd Congo, the Democratic Republic of the cf Central African Republic cg Congo ch Switzerland ci Cote d’Ivoire cl Chile cm Cameroon cn China co Colombia cr Costa Rica cu Cuba cv Cape Verde cy Cyprus cz Czech Republic de Germany dj Djibouti dk Denmark do Dominican Republic dz Algeria ec Ecuador ee Estonia eg Egypt eh Western Sahara er Eritrea es Spain et Ethiopia fi Finland fr France ga Gabon gb United Kingdom ge Georgia gf French Guiana gh Ghana gl Greenland gm Gambia gn Guinea gq Equatorial Guinea gr Greece gt Guatemala gu Guam gw Guinea-Bissau gy Guyana hk Hong Kong hn Honduras hr Croatia ht Haiti hu Hungary id Indonesia ie Ireland il Israel in India iq Iraq ir Iran, Islamic Republic of is Iceland it Italy jm Jamaica jo Jordan jp Japan ke Kenya kg Kyrgyzstan kh Cambodia kp Korea, Democratic People’s Republic of kr Korea, Republic of kw Kuwait kz Kazakhstan la Lao People’s Democratic Republic lb Lebanon li Liechtenstein lk Sri Lanka lr Liberia ls Lesotho lt Lithuania lu Luxembourg lv Latvia ly Libyan Arab Jamahiriya ma Morocco mc Monaco md Moldova, Republic of me Montenegro mg Madagascar mk Macedonia, the former Yugoslav Republic of ml Mali mm Myanmar mn Mongolia mo Macao mr Mauritania mt Malta mu Mauritius mv Maldives mw Malawi mx Mexico my Malaysia mz Mozambique na Namibia ne Niger ng Nigeria ni Nicaragua nl Netherlands no Norway np Nepal nz New Zealand om Oman pa Panama pe Peru pg Papua New Guinea ph Philippines pk Pakistan pl Poland pr Puerto Rico ps Palestine, State of pt Portugal py Paraguay re Reunion ro Romania rs Serbia ru Russian Federation rw Rwanda sa Saudi Arabia sc Seychelles sd Sudan se Sweden sg Singapore sh Saint Helena, Ascension and Tristan da Cunha si Slovenia sk Slovakia sl Sierra Leone sm San Marino sn Senegal so Somalia sr Suriname st Sao Tome and Principe sv El Salvador sy Syrian Arab Republic sz Swaziland td Chad tg Togo th Thailand tj Tajikistan tl Timor-Leste tm Turkmenistan tn Tunisia tr Turkey tw Taiwan (Republic of China) tz Tanzania, United Republic of ua Ukraine ug Uganda us United States uy Uruguay uz Uzbekistan va Holy See (Vatican City State) ve Venezuela, Bolivarian Republic of vn Viet Nam ye Yemen yt Mayotte za South Africa zm Zambia zw Zimbabwe
示例 1:根据数据绘制国家。
Python3
# import pygal library
import pygal
# create a world map
worldmap = pygal.maps.world.World()
# set the title of the map
worldmap.title = 'Countries'
# adding the countries
worldmap.add('Random Data', {
'aq' : 10,
'cd' : 30,
'de' : 40,
'eg' : 50,
'ga' : 45,
'hk' : 23,
'in' : 70,
'jp' : 54,
'nz' : 41,
'kz' : 32,
'us' : 66
})
# save into the file
worldmap.render_to_file('abc.svg')
print("Success")
Python3
# import pygal
import pygal
# import Style class from pygal.style
from pygal.style import Style
# create a Style object
custom_style = Style( colors = ('#FF0000' , '#0000FF' ,
'#00FF00' , '#000000',
'#FFD700'))
# create a world map,
# Style class is used for using
# the custom colours in the map,
worldmap = pygal.maps.world.World(style
= custom_style)
# set the title of the map
worldmap.title = 'Some Countries Starting from Specific Letters'
# hex code of colours are used
# for every .add() called
worldmap.add('"E" Countries',
['ec', 'ee', 'eg', 'eh',
'er', 'es','et'])
worldmap.add('"F" Countries',
['fr', 'fi'])
worldmap.add('"P" Countries',
['pa', 'pe', 'pg', 'ph', 'pk',
'pl','pr', 'ps', 'pt', 'py'])
worldmap.add('"Z" Countries',
['zm', 'zw'])
worldmap.add ('"A" Countries' ,
['ad','ae', 'af', 'al', 'am', 'ao',
'aq', 'ar', 'at', 'au', 'az'],
color = 'black')
# save into the file
worldmap.render_to_file('abc.svg')
print("Success")
Python3
# import pygal library
import pygal
# create a world map
worldmap = pygal.maps.world.SupranationalWorld()
# set the title of map
worldmap.title = 'Continents'
# adding the continents
worldmap.add('Africa', [('africa')])
worldmap.add('North america', [('north_america')])
worldmap.add('Oceania', [('oceania')])
worldmap.add('South america', [('south_america')])
worldmap.add('Asia', [('asia')])
worldmap.add('Europe', [('europe')])
worldmap.add('Antartica', [('antartica')])
# save into the file
worldmap.render_to_file('abc.svg')
print("Success")
输出:
示例 2:绘制国家 与标签。
Python3
# import pygal
import pygal
# import Style class from pygal.style
from pygal.style import Style
# create a Style object
custom_style = Style( colors = ('#FF0000' , '#0000FF' ,
'#00FF00' , '#000000',
'#FFD700'))
# create a world map,
# Style class is used for using
# the custom colours in the map,
worldmap = pygal.maps.world.World(style
= custom_style)
# set the title of the map
worldmap.title = 'Some Countries Starting from Specific Letters'
# hex code of colours are used
# for every .add() called
worldmap.add('"E" Countries',
['ec', 'ee', 'eg', 'eh',
'er', 'es','et'])
worldmap.add('"F" Countries',
['fr', 'fi'])
worldmap.add('"P" Countries',
['pa', 'pe', 'pg', 'ph', 'pk',
'pl','pr', 'ps', 'pt', 'py'])
worldmap.add('"Z" Countries',
['zm', 'zw'])
worldmap.add ('"A" Countries' ,
['ad','ae', 'af', 'al', 'am', 'ao',
'aq', 'ar', 'at', 'au', 'az'],
color = 'black')
# save into the file
worldmap.render_to_file('abc.svg')
print("Success")
输出
示例 3:绘制大陆。
Python3
# import pygal library
import pygal
# create a world map
worldmap = pygal.maps.world.SupranationalWorld()
# set the title of map
worldmap.title = 'Continents'
# adding the continents
worldmap.add('Africa', [('africa')])
worldmap.add('North america', [('north_america')])
worldmap.add('Oceania', [('oceania')])
worldmap.add('South america', [('south_america')])
worldmap.add('Asia', [('asia')])
worldmap.add('Europe', [('europe')])
worldmap.add('Antartica', [('antartica')])
# save into the file
worldmap.render_to_file('abc.svg')
print("Success")
输出: