figure.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import plotly.graph_objects as go
  2. # 定义节点和颜色
  3. labels = ["No chronic disease", "One chronic disease", "Comorbidity", "Death"]
  4. colors = ["#4C78A8", "#F58518", "#E45756", "#72B7B2"]
  5. # 定义各年龄段的连接
  6. source = [0, 0, 1, 1, 2, 2, 0, 1, 2, # 45~54
  7. 0, 0, 1, 1, 2, 2, 0, 1, 2, # 55~64
  8. 0, 0, 1, 1, 2, 2, 0, 1, 2] # >=65
  9. target = [1, 2, 2, 0, 1, 0, 3, 3, 3,
  10. 1, 2, 2, 0, 1, 0, 3, 3, 3,
  11. 1, 2, 2, 0, 1, 0, 3, 3, 3]
  12. # 定义各连接的值
  13. values = [1, 1, 1, 1, 1, 1, 1, 1, 1, # 45~54
  14. 1.18, 1.57, 1.26, 1.36, 1.46, 1.10, 1.32, 0.89, 2.59, # 55~64
  15. 1.34, 2.04, 1.52, 1.71, 1.78, 1.44, 9.09, 10.57, 7.52] # >=65
  16. # 创建桑基图
  17. fig = go.Figure(go.Sankey(
  18. node=dict(
  19. pad=15,
  20. thickness=20,
  21. line=dict(color="black", width=0.5),
  22. label=labels,
  23. color=colors
  24. ),
  25. link=dict(
  26. source=source,
  27. target=target,
  28. value=values,
  29. color=["#4C78A8", "#F58518", "#E45756"] * 3 # 不同颜色表示年龄段
  30. )
  31. ))
  32. fig.update_layout(title_text="Transition of Health States Across Age Groups", font_size=10)
  33. fig.show()