Pythonでローズチャートなどの一般的な流行マップを描画する方法

新しい王冠の流行は数か月間続いています。現在、中国での流行は基本的に抑制されており、ヨーロッパとアメリカの国々が発生期にあります。多くのWebサイトがさまざまな流行統計を提供していることがわかります。今日、Python pyechartsフレームワークを使用していますいくつかの一般的な統計を描画します。

バライラスト


最初に、少し前に比較的暑かったナイチンゲールローズチャートを描画します。データソースは、インターフェイス `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,color= '#003399'),
                                               pos_right= 'center',pos_left= '53%',pos_top= '62%',pos_bottom='center'
                                              ),
                     legend_opts=opts.LegendOpts(is_show=False))
# 设置系列配置和颜色
pie.set_series_opts(label_opts=opts.LabelOpts(is_show=True, position='inside', font_size=12,
                                              formatter='{b}:{c}', font_style='italic',
                                              font_family='Microsoft YaHei'))
pie.set_colors(color_series)
pie.render('南丁格尔玫瑰图.html')

レンダリングを見てください:

グローバル流行マップ


次に、世界的な流行状況のマップを作成します。さまざまな国の累積死亡数のデータを使用します。コードの実装は次のとおりです。
url = 'https://lab.isaaclin.cn/nCoV/api/area'
data = requests.get(url).json()
oversea_confirm = []
for item in data['results']:
    if item['countryEnglishName']:
        oversea_confirm.append((item['countryEnglishName']
                                .replace('United States of America', 'United States')
                                .replace('United Kiongdom', 'United Kingdom'),
                                item['deadCount']))
world_map = (
        Map(init_opts=opts.InitOpts(theme='dark'))
        .add('累计死亡人数', oversea_confirm, 'world',is_map_symbol_show=False, is_roam=False)
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False, color='#fff'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title='全球疫情累计死亡人数地图'),
            legend_opts=opts.LegendOpts(is_show=False),
            visualmap_opts=opts.VisualMapOpts(max_=2700,
                                              is_piecewise=True,
                                              pieces=[
                                                {"max": 99999, "min": 10000, "label": "10000人及以上", "color": "#8A0808"},
                                                {"max": 9999, "min": 1000, "label": "1000-9999人", "color": "#B40404"},
                                                {"max": 999, "min": 500, "label": "500-999人", "color": "#DF0101"},
                                                {"max": 499, "min": 100, "label": "100-499人", "color": "#F78181"},
                                                {"max": 99, "min": 10, "label": "10-99人", "color": "#F5A9A9"},
                                                {"max": 9, "min": 0, "label": "1-9人", "color": "#FFFFCC"},
                                              ])
        )
    )
world_map.render(path='全球疫情地图.html')

レンダリングを見てください:

中国の流行地図


次に、中国の流行状況のマップを描画します。このデータは、各省で診断された累積人数に基づいています。コードの実装は次のとおりです。
url = 'https://lab.isaaclin.cn/nCoV/api/area'
data = requests.get(url).json()
province_data = []
for item in data['results']:
    if item['countryName'] == '中国':
        province_data.append((item['provinceShortName'], item['confirmedCount']))
china_map = (
        Map(init_opts=opts.InitOpts(theme='dark'))
        .add('确诊人数', province_data, 'china',is_map_symbol_show=False,  is_roam=False)
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True, color='#ffffff'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="中国疫情累计确诊人数地图"),
            legend_opts=opts.LegendOpts(is_show=False),
            visualmap_opts=opts.VisualMapOpts(max_=2000,
                                              is_piecewise=True,
                                              pieces=[
                                                  {"max": 9999, "min": 1000, "label": "1000-9999人", "color": "#B40404"},
                                                  {"max": 999, "min": 500, "label": "500-999人", "color": "#DF0101"},
                                                  {"max": 499, "min": 100, "label": "100-499人", "color": "#F78181"},
                                                  {"max": 99, "min": 10, "label": "10-99人", "color": "#F5A9A9"},
                                                  {"max": 9, "min": 0, "label": "1-9人", "color": "#FFFFCC"},
                                              ])
        )
)
china_map.render(path='中国疫情地图.html')

レンダリングを見てください:

ヒートマップ


ヒートマップの描画を続けましょう。中国のさまざまな省からデータを取得します。実装コードは次のとおりです。
url = 'https://lab.isaaclin.cn/nCoV/api/area'
data = requests.get(url).json()
cities_data = []
for item in data['results']:
    if item['countryName'] == '中国':
        if item['cities'] is not None:
            cities_data.extend(item['cities'])
hot_geo = (
        Geo(init_opts=opts.InitOpts(theme='dark'))
        .add_schema(maptype='china')
        .add('累计确诊人数',
             [(i['cityName'], i['currentConfirmedCount']) for i in cities_data
              if i['cityName'] in pyecharts.datasets.COORDINATES.keys()],
             type_='heatmap')
        .set_global_opts(
            title_opts=opts.TitleOpts(title='中国疫情热力图',
                                     pos_left='left'),
            legend_opts=opts.LegendOpts(is_show=False),
            visualmap_opts=opts.VisualMapOpts(is_show=True,
                                              is_piecewise=False,
                                              range_color=['#0ff', '#0f0', '#ff0', '#f00'])
        )
)
hot_geo.render(path='中国疫情热力图.html')

レンダリングを見てください:

ヒストグラム


次にヒストグラムを描きましょう。今回は省からデータを取得します。湖北省は診断数が最も多いため、この州のデータを使用します。
url = 'https://lab.isaaclin.cn/nCoV/api/area'
data = requests.get(url).json()
for item in data['results']:
    if item['provinceShortName'] == '湖北':
        hb_data = item['cities']
hb_bar = (
        Bar(init_opts=opts.InitOpts(theme='dark'))
        .add_xaxis([hd['cityName'] for hd in hb_data])
        .add_yaxis('累计确诊人数', [hd['confirmedCount'] for hd in hb_data])
        .add_yaxis('累计治愈人数', [hd['curedCount'] for hd in hb_data])
        .reversal_axis()
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="湖北新冠疫情确诊及治愈情况"),
            legend_opts=opts.LegendOpts(is_show=True)
                )
        )
hb_bar.render(path='湖北新冠疫情图.html')

レンダリングを見てください:

折れ線グラフ


現時点では、上記のインターフェースは時系列を返すデータを提供していませんが、データウェアハウスはGitHubで提供されています。JSONとCSVの2つの形式があります。GitHubの速度が遅いため、データのダウンロードに数回失敗しました。 、それでは、WeChatに直接表示されたデータを使用してみましょう。コードの実装は次のとおりです。
x_data = ['2-06', '2-13', '2-20', '2-27', '3-05', '3-12', '3-19', '3-26', '4-02', '4-09', '4-17']
# 现有确诊
y1_data = [20677, 46537, 49156, 36829, 22695, 13171, 6287, 2896, 987, 351, 122]
# 累计治愈
y2_data = [817, 4131, 11788, 26403, 41966, 51533, 58381, 61731, 63612, 64236, 63494]
line = (Line()
        .add_xaxis(x_data)
        .add_yaxis('现有确诊', y1_data, color='#10aeb5')
        .add_yaxis('累计治愈', y2_data, color='#e83132')
        .set_series_opts(label_opts=opts.LabelOpts(is_show=True))
        .set_global_opts(
            title_opts=opts.TitleOpts(title='中国疫情随时间变化趋势')
       ))

line.render(path='中国疫情折线图.html')

レンダリングを見てください:

ソースコードが必要な場合は、以下のQRコードをスキャンして、200418年に背景に返信できます。

おすすめ

転載: www.cnblogs.com/ityard/p/12730177.html