按照書籍中
代碼
americas.py
import pygal
wm = pygal.Worldmap()
wm.title = 'North,Central,and South America'#設置該地圖的title屬性
wm.add('North America',['ca','mx','us'])#使用ADD黨閥接收一個標簽和一個列表
wm.add('Central America',['bz','cr','gt','hn','ni','pa','sv'])
wm.add('South America',['ar','bo','br','cl','co','ec','gf'])
wm.render_to_file('americas.svg')
輸出結果為
AttributeError: module 'pygal' has no attribute 'Worldmap'
因為老舊模塊已經(jīng)不存在
修改為
americas.py
import pygal.maps.world
wm = pygal.maps.world.World()#創(chuàng)建一個worldmap的勢力
wm.title = 'North,Central,and South America'#設置該地圖的title屬性
wm.add('North America',['ca','mx','us'])#使用ADD黨閥接收一個標簽和一個列表
wm.add('Central America',['bz','cr','gt','hn','ni','pa','sv'])
wm.add('South America',['ar','bo','br','cl','co','ec','gf'])
wm.render_to_file('americas.svg')
文件會生成一個americas.svg文件亭敢,使用瀏覽器打開即可訪問蜀漆。
程序
na_populations
import pygal
wm = pygal.Worldmap()
wm.title = 'Population of Countries in North America'
wm.add('North America',{'ca':34126000,'us':30934900,'mx':113423000})
wm.render_to_file('na_populations.svg')
報錯
AttributeError: module 'pygal' has no attribute 'Worldmap'
改為
import pygal.maps.world
wm = pygal.maps.world.World()
wm.title = 'Population of Countries in North America'
wm.add('North America',{'ca':34126000,'us':30934900,'mx':113423000})
wm.render_to_file('na_populations.svg')