小奥的学习笔记

  • Home
  • Learning & Working
    • Speech Enhancement Notes
    • Programming language
    • Computer & DL
    • MOOC
  • Life
    • Life Time
    • Thinking & Comprehension
    • Volunteer
    • Plan
    • Travel
  • Footprints
  • GuestBook
  • About
    • About Me
    • 个人履历
    • 隐私策略
  1. 首页
  2. Study-notes
  3. Programming language
  4. Python
  5. 正文

基于python绘制世界人口地图

2017年10月22日 1458点热度 0人点赞 0条评论

主程序:

# -*- coding: utf-8 -*-
"""
Created on Sun Oct 22 19:57:24 2017

@author: dqhpl
"""
import json
import pygal.maps.world
from country_codes import get_country_code
from pygal.style import RotateStyle as RS, LightColorizedStyle as LCS #利用as创建简写,便于书写

#将数据加载到一个列表之中
filename = 'population_data.json'
with open(filename) as f:
    pop_data = json.load(f)
#创建一个包含人口数量的字典
cc_populations = {}
#打印每个国家2015年的人口数量
for pop_dict in pop_data:
    if pop_dict['Year'] =='2010':
        country = pop_dict['Country Name']
        population= int(float(pop_dict['Value']))#由于直接转int会出错。所以先转float再转int
        #print(country_name + ": " + str(population))
        code= get_country_code(country)
        if code:
            cc_populations = population

#根据人口数量将所有的国家分为三组
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 = RS('#336699', base_style=LCS)#创建了一种基调,LCS指的是用亮色风格
wm = pygal.maps.world.World(style= wm_style)#pygal升级,所以需要注意                     
wm.title = 'World Population in 2010, by Country'
wm.add('0-10m', cc_pops_1)
wm.add('10m-1bn', cc_pops_2)
wm.add('>10bn', cc_pops_3)

wm.render_to_file('world_population.svg')

country_codes.py

# -*- coding: utf-8 -*-
"""
Created on Sun Oct 22 20:09:53 2017

@author: dqhpl
"""
# =============================================================================
# The i18n module was removed in pygal-2.0.0, 
#however, it can now be found in the pygal_maps_world plugin.
# 
# You can install that with pip install pygal_maps_world. 
#Then you can access COUNTRIES as pygal.maps.world.COUNTRIES:
# 
# from pygal.maps.world import COUNTRIES
# Whats left of the i18n module can be imported with:
# 
# from pygal_maps_world import i18n
# =============================================================================
from pygal_maps_world import i18n
from pygal.maps.world import COUNTRIES

def get_country_code(country_name):
    """根据指定的国家,返回pygal使用的两个字母的国别码"""
    for code, name in COUNTRIES.items():
        if name == country_name:
            return code
    #如果没有找到指定的国家,就返回None
    return None

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: Python python学习
最后更新:2017年10月22日

davidcheung

这个人很懒,什么都没留下

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

搜索
欢迎关注我的个人公众号
最新 热点 随机
最新 热点 随机
DEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架 奥地利匈牙利九日游旅程 论文阅读之Study of the General Kalman Filter for Echo Cancellation 小奥看房之鸿荣源珈誉府 杭州往返旅途及西溪喜来登和万怡的体验报告 2022年的第一篇碎碎念
奥地利匈牙利九日游旅程论文阅读之Study of the General Kalman Filter for Echo CancellationDEEPFILTERNET:一种基于深度滤波的全频带音频低复杂度语音增强框架
[leetcode]Same Tree 四六级写作经典句型汇总 16 类 《Python语言程序设计基础》课后习题程序整理(第5~7章) 新青年报[New Youth]第八期(高考毕业特刊)发布! 新青年报[合集]发布! 每日一感0810:迷信害死人~~
标签聚合
鸟哥的linux私房菜 学习 Python 高中 生活 linux leetcode 算法 Java python学习
最近评论
davidcheung 发布于 5 个月前(02月09日) The problem has been fixed. May I ask if you can s...
tk88 发布于 5 个月前(02月07日) Hmm is anyone else having problems with the pictur...
cuicui 发布于 9 个月前(10月20日) :wink:
niming 发布于 10 个月前(09月19日) 同级校友,能刷到太巧了
davidcheung 发布于 2 年前(08月16日) 我得找一下我之前整理的word文档看一下,如果找到了我就更新一下这篇文章。
Nolan 发布于 2 年前(07月25日) 您的笔记非常有帮助。贴图不显示了,可以更新一下吗?
davidcheung 发布于 3 年前(06月19日) 到没有看webrtc的代码。现在主要在看我们公司的代码了。。。只是偶尔看一看webrtc的东西。。。
aobai 发布于 3 年前(03月13日) gain_change_hangover_ 应该是每三个block 只能够调整一次,这样保证每帧...
匿名 发布于 5 年前(12月30日) 烫
小奥 发布于 5 年前(12月12日) webRTC里面的NS本身我记得就是在C++里面呀

COPYRIGHT © 2025 小奥的学习笔记. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

陕ICP备19003234号-1

鲁公网安备37120202000100号