python如何绘制两点间连线_如何用 Python 绘制玫瑰图等常见疫情图

6e44d132661c903f0cc9d5dad4b61392.png

新冠疫情已经持续好几个月了,目前,我国疫情已经基本控制住了,我们会看到很多网站都提供了多种疫情统计图,今天我们使用 Python 的 pyecharts 框架来绘制一些比较常见的统计图。

1. 玫瑰图

首先,我们来绘制前段时间比较火的南丁格尔玫瑰图,数据来源我们通过接口 https://lab.isaaclin.cn/nCoV/zh 来获取,我们取疫情中死亡人数超过 2000 的国家的数据,实现代码如下:

url = 'https://lab.isaaclin.cn/nCoV/api/area'
data_json = requests.get(url).json()
country_list = []
count_list = []
ds = {}
for item in data_json['results']:
    if item['countryEnglishName']:
        if item['deadCount'] is not None and item['countryName'] is not None:
            if int(item['deadCount']) > 2000:
                d = {
    item['countryName']:item['deadCount']}
                ds.update(d)
ds = dict(sorted(ds.items(), key = lambda k: k[1]))
# 名称有重复的,把国家名作为 key 吧
country_list = ds.keys()
count_list = ds.values()
# 随机颜色生成
def randomcolor(kind):
    colors = []
    for i in range(kind):
        colArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
        color = ""
        for i in range(6):
            color += colArr[random.randint(0, 14)]
        colors.append("#" + color)
    return colors
color_series = randomcolor(len(count_list))
# 创建饼图
pie = Pie(init_opts=opts.InitOpts(width='800px', height='900px'))
# 添加数据
pie.add("", [list(z) for z in zip(country_list, count_list)],
        radius=['20%', '100%'],
        center=['60%', '65%'],
        rosetype='area')
# 设置全局配置
# pie.set_global_opts(title_opts=opts.TitleOpts(title='南丁格尔玫瑰图'),
#                     legend_opts=opts.LegendOpts(is_show=False))
# 设置全局配置项
pie.set_global_opts(title_opts=opts.TitleOpts(title='全球新冠疫情',subtitle='死亡人数超过n2000 的国家',
                                               title_textstyle_opts=opts.TextStyleOpts(font_size=15,color= '#0085c3'),
                                               subtitle_textstyle_opts= opts.TextStyleOpts(font_size=15,col
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值