最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python學(xué)習(xí)之用pygal畫世界地圖實(shí)例

 更新時(shí)間:2017年12月07日 16:51:36   作者:waiwai3  
這篇文章主要介紹了Python學(xué)習(xí)之用pygal畫世界地圖實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。

有關(guān)pygal的介紹和安裝,大家可以參閱pip和pygal的安裝實(shí)例教程,然后利用pygal實(shí)現(xiàn)畫世界地圖。代碼如下:

#coding=utf-8 
import json 
import pygal.maps.world 
#Pygal樣式保存在模塊style中,包括RotateStyle調(diào)整顏色和LightColorizedStyle加亮顏色 
#也可以寫成from pygal.style import LightColorizedStyle, RotateStyle 
import pygal.style 
from country_codes import get_country_code 
 
 #將數(shù)據(jù)加載到列表中 
filename='population_data.json' 
with open(filename) as f: 
  pop_data=json.load(f) 
 
 #創(chuàng)建一個(gè)包含人口數(shù)量的字典 
cc_populations={} 
for pop_dict in pop_data: 
  if pop_dict['Year'][:4]=='2010': 
    country_name=pop_dict['Country Name'] 
    poplulation=int(pop_dict['Value']) 
    code=get_country_code(country_name) 
    if code: 
      cc_populations[code]=poplulation 
 
 
 #根據(jù)人口數(shù)量分組 
cc_pops_1,cc_pops_2,cc_pops_3={},{},{} 
for cc,pop in cc_populations.items(): 
  if pop < 10000000: 
    cc_pops_1[cc] = pop 
  elif pop < 1000000000: 
    cc_pops_2[cc] = pop 
  else: 
    cc_pops_3[cc] = pop 
print len(cc_pops_1),len(cc_pops_2),len(cc_pops_3) 
 
 
 #畫圖 
#wm_style是一個(gè)樣式對(duì)象,第一個(gè)實(shí)參表示顏色,十六進(jìn)制格式,分別表示紅綠藍(lán)的分量(RGB),第二個(gè)實(shí)參表示加亮顏色主題 
wm_style=pygal.style.RotateStyle('#3399AA',base_style=pygal.style.LightColorizedStyle) 
wm=pygal.maps.world.World(style=wm_style) 
wm.title="World Population in 2010,by Country" 
 
#add接收一個(gè)標(biāo)簽和一個(gè)列表,此例中標(biāo)簽為人口數(shù)量區(qū)間,列表是國(guó)家和人口數(shù)量的列表 
wm.add('0-10m',cc_pops_1) 
wm.add('10m-1bn',cc_pops_2) 
wm.add('>1bn',cc_pops_3) 
 
wm.render_to_file('world_population.svg') 

注意:

1.有些書上寫的國(guó)別碼是在pygal.i18n中的COUNTRIES,我importpygal.i18n會(huì)報(bào)錯(cuò),改為pygal.maps.world模塊成功

from pygal.maps.world import COUNTRIES 

或者使用如下方式,不是很明白這兩種有什么區(qū)別,都運(yùn)行正常

from pygal_maps_world.i18n import COUNTRIE 

2.pygal提供的圖標(biāo)類型WorldMap我也沒找到,創(chuàng)建實(shí)例wm=pygal.WorlgMap()報(bào)錯(cuò),改為

wm=pygal.maps.world.World() 

3.wm_style是一個(gè)樣式對(duì)象,用來展示圖的顏色和亮度等,LightColorizedStyle和RotateStyle

但是只使用LightColorizedStyle,無法控制使用的顏色,pygal會(huì)選擇默認(rèn)基色。(效果如圖1)

wm_style=pygal.style.LightColorizedStyle 

設(shè)置自己喜歡的顏色,也可以不寫base_style(效果如圖2,my_style如下)

wm_style=pygal.style.RotateStyle('#3399AA',base_style=pygal.style.LightColorizedStyle) 

總結(jié)

以上就是本文關(guān)于Python學(xué)習(xí)之用pygal畫世界地圖實(shí)例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

相關(guān)文章

最新評(píng)論

巴南区| 沂水县| 霞浦县| 略阳县| 酉阳| 樟树市| 上栗县| 灌云县| 阳城县| 清涧县| 怀化市| 九江县| 乐亭县| 黑水县| 尤溪县| 宁蒗| 天全县| 泰和县| 深泽县| 罗平县| 尚志市| 普安县| 安吉县| 都江堰市| 徐闻县| 永清县| 贵港市| 南乐县| 三江| 建宁县| 陆丰市| 丰原市| 湄潭县| 勃利县| 孟津县| 额济纳旗| 濮阳市| 海丰县| 绥德县| 温宿县| 通州市|