1 回答

TA貢獻1836條經驗 獲得超4個贊
在和 中go.Sankey()
設置和調整 x和arrangement='snap'
y 位置。以下設置將按要求放置您的節點。x=<list>
y=<list>
陰謀:
請注意,此示例中未明確設置 y 值。一旦一個公共 x 值有多個節點,y 值將自動調整以使所有節點顯示在相同的垂直位置。如果您確實想明確設置所有位置,只需設置arrangement='fixed'
編輯:
我添加了一個自定義函數nodify()
,該函數將相同的 x 位置分配給具有共同結尾的標簽名稱,例如'0'
in ['home0', 'page_a0', 'page_b0']
?,F在,如果你作為一個例子改變page_c1
你page_c2
會得到這個:
完整代碼:
import plotly.graph_objects as go
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]
def nodify(node_names):
node_names = unique_list
# uniqe name endings
ends = sorted(list(set([e[-1] for e in node_names])))
# intervals
steps = 1/len(ends)
# x-values for each unique name ending
# for input as node position
nodes_x = {}
xVal = 0
for e in ends:
nodes_x[str(e)] = xVal
xVal += steps
# x and y values in list form
x_values = [nodes_x[n[-1]] for n in node_names]
y_values = [0.1]*len(x_values)
return x_values, y_values
nodified = nodify(node_names=unique_list)
# plotly setup
fig = go.Figure(data=[go.Sankey(
arrangement='snap',
node = dict(
pad = 15,
thickness = 20,
line = dict(color = "black", width = 0.5),
label = unique_list,
color = "blue",
x=nodified[0],
y=nodified[1]
),
link = dict(
source = sources,
target = targets,
value = values
))])
fig.show()
添加回答
舉報