使用Python pygal模块绘制世界地图示例

栏目: Python · 发布时间: 5年前

内容简介:Linux系统环境:Ubuntu 18.04 LTSPython版本:Python 3.6.7linuxidc@linuxidc:~/linuxidc.com$

Linux系统环境:Ubuntu 18.04 LTS

Python版本:Python 3.6.7

首先安装:pygal_maps_world

linuxidc@linuxidc:~/linuxidc.com$ pip3 install pygal_maps_world -i https://pypi.douban.com/simple/

Collecting pygal_maps_world

Downloading https://pypi.doubanio.com/packages/a0/6e/2471c9db8e5402f844cdab2e6270fb315ee83e308d4977e356a04ca76c36/pygal_maps_world-1.0.2.tar.gz (270kB)

100% |████████████████████████████████| 276kB 1.3MB/s

Collecting pygal>=1.9.9 (from pygal_maps_world)

Downloading https://pypi.doubanio.com/packages/5f/b7/201c9254ac0d2b8ffa3bb2d528d23a4130876d9ba90bc28e99633f323f17/pygal-2.4.0-py2.py3-none-any.whl (127kB)

100% |████████████████████████████████| 133kB 1.2MB/s

Building wheels for collected packages: pygal-maps-world

Running setup.py bdist_wheel for pygal-maps-world ... done

Stored in directory: /home/linuxidc/.cache/pip/wheels/87/87/49/4bd1d5732bdb111ca75d8fdb14390ff9993735067a1888e824

Successfully built pygal-maps-world

Installing collected packages: pygal, pygal-maps-world

Successfully installed pygal-2.4.0 pygal-maps-world-1.0.2

此时可以访问pygal.maps.world模块,并指定代码绘制国家

示例代码:

import pygal

from pygal_maps_world.i18n import COUNTRIES

def word_country_map():

"""世界各国"""

wm = pygal.maps.world.World()

wm.force_uri_protocol = 'http'

wm.title = "世界地图"

for code,name in COUNTRIES.items():

wm.add(name,code)

wm.add('Yemen',{'ye':'Yemem'})

wm.render_to_file('linuxidc.svg')

def one_country_map():

wm = pygal.maps.world.World()

wm.force_uri_protocol = 'http'

wm.title = '世界地图(一国)'

wm.add('North America', ['ca', 'mx', 'us'])

wm.render_to_file('linuxidc.com.svg')

if __name__ == '__main__':

wm = pygal.maps.world.World()

wm.title = 'North,Central,and South America'

wm.add('North America', ['ca', 'mx', 'us'])

wm.add('Central America', ['bz', 'cr', 'gt', 'hn', 'ni', 'pa', 'sv'])

wm.add('South America', ['ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf',

'gy', 'pe', 'py', 'sr', 'uy', 've'])

wm.render_to_file('www.linuxidc.com.svg')

word_country_map()

one_country_map()

绘制图如下:

使用Python pygal模块绘制世界地图示例

使用Python pygal模块绘制世界地图示例

使用Python pygal模块绘制世界地图示例

Countries

import pygal.maps.world

worldmap_chart = pygal.maps.world.World()

worldmap_chart.title = 'Some countries'

worldmap_chart.add('F countries', ['fr', 'fi'])

worldmap_chart.add('M countries', ['ma', 'mc', 'md', 'me', 'mg',

'mk', 'ml', 'mm', 'mn', 'mo',

'mr', 'mt', 'mu', 'mv', 'mw',

'mx', 'my', 'mz'])

worldmap_chart.add('U countries', ['ua', 'ug', 'us', 'uy', 'uz'])

worldmap_chart.render_to_file('www.linuxidc.com.svg')

如下图:

使用Python pygal模块绘制世界地图示例

指定某个国家的值:

import pygal.maps.world

worldmap_chart = pygal.maps.world.World()

worldmap_chart.title = 'Minimum deaths by capital punishement (source: Amnesty International)'

worldmap_chart.add('In 2012', {

'af': 14,

'bd': 1,

'by': 3,

'cn': 1000,

'gm': 9,

'in': 1,

'ir': 314,

'iq': 129,

'jp': 7,

'kp': 6,

'pk': 1,

'ps': 6,

'sa': 79,

'so': 6,

'sd': 5,

'tw': 6,

'ae': 1,

'us': 43,

'ye': 28

})

worldmap_chart.render_to_file('www.linuxidc.com.svg')

如下图:

使用Python pygal模块绘制世界地图示例

Continents

获取洲地图

import pygal.maps.world

supra = pygal.maps.world.SupranationalWorld()

supra.add('Asia', [('asia', 1)])

supra.add('Europe', [('europe', 1)])

supra.add('Africa', [('africa', 1)])

supra.add('North america', [('north_america', 1)])

supra.add('South america', [('south_america', 1)])

supra.add('Oceania', [('oceania', 1)])

supra.add('Antartica', [('antartica', 1)])

supra.render_to_file('www.linuxidc.com.svg')

如下图:

使用Python pygal模块绘制世界地图示例

国家代码列表:

code Country
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
洲列表:
code name
asia Asia
europe Europe
africa Africa
north_america North America
south_america South America
oceania Oceania
antartica Antartica

更多 Python 相关信息见 Python 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2019-04/158244.htm


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

产品心经

产品心经

闫荣 / 机械工业出版社 / 2014-9-30 / 59

产品经理如何才能迅速地、全方位地提升自己的能力,从而打造出让用户尖叫并疯狂爱上的产品?有没有捷径?从成功的、有经验的产品经理的实践真知和智慧中学习是一个很好的途径!本书就是一位拥有近10年产品经验的资深产品经理的实践真知和智慧的结晶,从产品经理核心素养、产品认知、战略与规划、精益开发、需求分析与管理、用户体验、精细运营7大方面,系统梳理了能全面、迅速提升产品经理能力,从而打造出让用户尖叫的产品的5......一起来看看 《产品心经》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具