如何在python中在x轴上绘制带有时间的sankey图?

时间:2021-04-08 09:56:29

标签: python python-3.x plotly

我有以下 pandas 数据框:

df_dict = {'index': [15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  26, 27, 28, 29, 30, 31, 32, 33, 34],
 'columns': ['cluster', 'week', 'color_line'],
 'data': [[3, 1, 'green'],
  [3, 2, 'green'],
  [3, 3, 'green'],
  [3, 4, 'green'],
  [3, 5, 'green'],
  [3, 6, 'green'],
  [4, 7, 'green'],
  [3, 8, 'yellow'],
  [3, 9, 'yellow'],
  [4, 10, 'yellow'],
  [3, 11, 'green'],
  [3, 12, 'yellow'],
  [3, 13, 'yellow'],
  [4, 14, 'yellow'],
  [4, 15, 'red'],
  [4, 16, ' orange'],
  [3, 17, 'yellow'],
  [3, 18, 'green'],
  [4, 19, 'red'],
  [3, 20, 'green']]}

to_plot_2 = pd.DataFrame(index=df_dict['index'], columns=df_dict['columns'], data=df_dict['data'])

我想创建一个桑基图,它会显示从 clustercluster_target(它只是 shiftcluster } 列),由 color_line

着色

我已经试过了:

import plotly.graph_objects as go
fig = go.Figure(data=[go.Sankey(
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = [0,1,2,3,4],
      color = "blue"
    ),
    link = dict(
      source = to_plot_2['cluster'],
      target = to_plot_2['cluster_target'],
      value = [1] * len(to_plot_2),
      color = to_plot_2['color_line']
  ))])


fig.show()

我不知道如何在 x 轴上添加 week 元素,有什么想法吗?

更新

我已经试过了

fig = go.Figure(data=[go.Sankey(
    arrangement='snap',
    node = dict(
      pad = 20,
#       thickness = 20,
#       line = dict(color = "black", width = 0.5),
      label = list(to_plot_2['cluster'].astype(str)),
      color = "blue",
      x = [i * 1/(to_plot_2.week.max()) for i in list(range(0,to_plot_2.week.max(),1))],
      y = list(to_plot_2['cluster'] * (1/5))
    ),
    link = dict(
      source = list(range(0,to_plot_2.week.max())),
      target = list(range(1,to_plot_2.week.max()+1)),
      value = [1] * len(to_plot_2),
      color = to_plot_2['color_line']
  ))])


fig.show()

但输出看起来像这样:

enter image description here

所以标签不在节点上,而是在链接上。有什么想法吗?

0 个答案:

没有答案
相关问题