在python中,如何在x轴上绘制带时间的sankey绘图?
我有以下在python中,如何在x轴上绘制带时间的sankey绘图?,python,python-3.x,plotly,Python,Python 3.x,Plotly,我有以下pandasdataframe: 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, 'gre
pandas
dataframe:
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'])
我想创建一个sankey图,它将显示集群
到集群目标
(这只是集群
列的移位
),由颜色线
我试过这个:
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()
但输出如下所示:
因此,标签不在节点上,而是在链接上。有什么想法吗?请分享您的数据,如@vestland编辑的问题。tnx