样本数据如下:
unique_list = ['home0', 'page_a0', 'page_b0', 'page_a1', 'page_b1',
'page_c1', 'page_b2', 'page_a2', 'page_c2', 'page_c3']
sources = [0, 0, 1, 2, 2, 3, 3, 4, 4, 7, 6]
targets = [3, 4, 4, 3, 5, 6, 8, 7, 8, 9, 9]
values = [2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2]
使用来自the documentation的示例代码
fig = go.Figure(data=[go.Sankey(
node = dict(
pad = 15,
thickness = 20,
line = dict(color = "black", width = 0.5),
label = unique_list,
color = "blue"
),
link = dict(
source = sources,
target = targets,
value = values
))])
fig.show()
这将输出以下sankey图
但是,我希望得到在同一垂直列中以相同数字结尾的所有值,就像最左边的列的所有节点都以0结尾一样。我在docs中看到可以移动节点位置,但是我想知道除了手动输入x和y值之外,是否还有更干净的方法。谢谢你的帮助
在
go.Sankey()
中设置arrangement='snap'
并调整x=<list>
和y=<list>
中的x和y位置。以下设置将根据请求放置节点绘图:
请注意,在本例中未明确设置y值。一旦公共x值有多个节点,y值将自动调整,以使所有节点显示在相同的垂直位置。如果您想显式地设置所有位置,只需设置
arrangement='fixed'
编辑:
我添加了一个自定义函数
nodify()
,该函数为具有公共结尾的标签名指定相同的x位置,例如['home0', 'page_a0', 'page_b0']
中的'0'
。现在,如果您将page_c1
更改为page_c2
,您将得到以下结果:完整代码:
相关问题 更多 >
编程相关推荐