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)
(备注:无港澳台人口数据)