Python Plotly武汉新型肺炎发展动态——桑基图

在这里插入图片描述

安装

pip install jupyter
pip install plotly

代码

import plotly
plotly.offline.init_notebook_mode(connected=True)

data = {
    "湖北": {
        "population": 57237727,
        "confirm": 270,
        "suspect": 11,
        "died": 9,
    },
    "北京": {
        "population": 19612368,
        "confirm": 10,
        "suspect": 0,
        "died": 0,
    },
    "广东": {
        "population": 104320459,
        "confirm": 26,
        "suspect": 1,
        "died": 0,
    },
    "上海": {
        "population": 23019196,
        "confirm": 9,
        "suspect": 10,
        "died": 0,
    },
    "浙江": {
        "population": 54426891,
        "confirm": 5,
        "suspect": 16,
        "died": 0,
    },
    "云南": {
        "population": 45966766,
        "confirm": 1,
        "suspect": 0,
        "died": 0,
    },
    "四川": {
        "population": 80417528,
        "confirm": 2,
        "suspect": 1,
        "died": 0,
    },
    "山东": {
        "population": 95792719,
        "confirm": 1,
        "suspect": 0,
        "died": 0,
    },
    "广西": {
        "population": 46023761,
        "confirm": 0,
        "suspect": 2,
        "died": 0,
    },
    "贵州": {
        "population": 34748556,
        "confirm": 0,
        "suspect": 1,
        "died": 0,
    },
    "安徽": {
        "population": 59500468,
        "confirm": 0,
        "suspect": 3,
        "died": 0,
    },
    "海南": {
        "population": 8671485,
        "confirm": 0,
        "suspect": 1,
        "died": 0,
    },
    "宁夏": {
        "population": 6301350,
        "confirm": 0,
        "suspect": 1,
        "died": 0,
    },
    "吉林": {
        "population": 27452815,
        "confirm": 0,
        "suspect": 1,
        "died": 0,
    },
    "江西": {
        "population": 44567797,
        "confirm": 2,
        "suspect": 0,
        "died": 0,
    },
    "天津": {
        "population": 12938693,
        "confirm": 2,
        "suspect": 0,
        "died": 0,
    },
    "河南": {
        "population": 94029939,
        "confirm": 1,
        "suspect": 0,
        "died": 0,
    },
    "重庆": {
        "population": 28846170,
        "confirm": 5,
        "suspect": 0,
        "died": 0,
    },
    "辽宁": {
        "population": 43746323,
        "confirm": 0,
        "suspect": 1,
        "died": 0,
    },
    "黑龙江": {
        "population": 38313991,
        "confirm": 0,
        "suspect": 1,
        "died": 0,
    },
    "湖南": {
        "population": 65700762,
        "confirm": 1,
        "suspect": 0,
        "died": 0,
    },
}

label = list(data.keys()) + ["确诊", "疑似", "死亡"]
print(label)

source = [i for i, v in enumerate(data.values()) if v["confirm"]] + [i for i, v in enumerate(data.values()) if v["suspect"]] + [len(label)-3 for v in data.values() if v["died"]] + [len(label)-2 for v in data.values() if v["died"]]
target = [len(label)-3 for v in data.values() if v["confirm"]] + [len(label)-2 for v in data.values() if v["suspect"]] + [len(label)-1 for v in data.values() if v["died"]] * 2
# 纯案例数
value = [v["confirm"] for v in data.values() if v["confirm"]] + [v["suspect"] for v in data.values() if v["suspect"]] + [v["died"] for v in data.values() if v["died"]] * 2
# 考虑人口
population_sum = sum([v["population"] for v in data.values() if v["confirm"]])
value = [v["confirm"]*(v["population"]/population_sum) for v in data.values() if v["confirm"]] + [v["suspect"]*(v["population"]/population_sum) for v in data.values() if v["suspect"]] + [v["died"]*(v["population"]/population_sum) for v in data.values() if v["died"]] * 2
print(source, len(source))
print(target, len(target))
print(value, len(value))
data_trace = dict(
    type='sankey',
    node=dict(pad=200,
              thickness=50,
              line=dict(color="black", width=0.5),
              label=label),
    link=dict(source=source,
              target=target,
              value=value))

layout = dict(
    title="武汉新型肺炎发展动态",
    font=dict(size=15, color='black'),
)
fig = dict(data=[data_trace], layout=layout)
plotly.offline.iplot(fig, validate=False)

(备注:无港澳台人口数据)

参考文献

  1. 实时更新|新型肺炎疫情地图
  2. 国家统计局>>普查数据
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XerCis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值