按照书籍中
代码
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'
因为老旧模块已经不存在
修改为
americas.py
import pygal.maps.world
wm = pygal.maps.world.World()#创建一个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')