如何在Python中用x轴时间绘制桑基图?

qua*_*ant 5 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'])
Run Code Online (Sandbox Code Playgroud)

我想创建一个桑基图,其中它将按周显示从 到 的过渡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()
Run Code Online (Sandbox Code Playgroud)

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

更新

我试过这个

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()
Run Code Online (Sandbox Code Playgroud)

但输出看起来像这样:

在此输入图像描述

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