plotly sankey - 是否能够将节点标签放在最后一层(列)的右侧
plotly sankey - is it able to put the node labels to the right for last layer(column)
我有一个桑基图,由 python plotly 创建,有 8 层(列)。
dataset = pd.read_csv('cleanSankey.csv')
labelListTemp1 = list(set(dataset.source.values))
labelListTemp2 = list(set(dataset.target.values))
labelList = labelListTemp1 + labelListTemp2
sankey_node = list(dict.fromkeys(labelList))
def nodify(node_names):
node_names = sankey_node
# 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=sankey_node)
fig = go.Figure(data=[go.Sankey( node = dict( pad=15,thickness=15,line = dict(color = "black", width = 0.5),label = labelList,color = 'grey',x=nodified[0],y=nodified[1] ),
link = dict(source = dataset.source.apply(lambda x: labelList.index(x)),
target = dataset.target.apply(lambda x: labelList.index(x)),
value = dataset.value),
arrangement='snap')])
fig.update_layout(title="performance Goal user behavior monitor",margin=dict(l=150))
fig.write_html('perfUXRGoal.html', auto_open=True)
问题:
标签位于节点的右侧,但最后一层的节点标签始终位于节点的左侧。
问题:
是否可以将标签也放在最后一层的节点右侧?
截图示例:
谢谢,
切丽
解决方法。有一个没有标签的附加层,所有目标节点都指向它。
import pandas as pd
import numpy as np
import plotly.graph_objects as go
links = [
{'source': 'start', 'target': 'A', 'value': 2},
{'source': 'A', 'target': 'B', 'value': 1},
{'source': 'A', 'target':'C', 'value':.5},
{'source': 'B', 'target':'', 'value':10**-10},
{'source': 'C', 'target':'', 'value':10**-10},
]
df = pd.DataFrame(links)
nodes = np.unique(df[["source","target"]], axis=None)
nodes = pd.Series(index=nodes, data=range(len(nodes)))
fig = go.Figure(
go.Sankey(
node={"label": nodes.index},
link={
"source": nodes.loc[df["source"]],
"target": nodes.loc[df["target"]],
"value": df["value"],
"color": np.where(df["target"].eq(""), "white", "rgba(30,30,30,.3)")
},
)
)
fig
我有一个桑基图,由 python plotly 创建,有 8 层(列)。
dataset = pd.read_csv('cleanSankey.csv')
labelListTemp1 = list(set(dataset.source.values))
labelListTemp2 = list(set(dataset.target.values))
labelList = labelListTemp1 + labelListTemp2
sankey_node = list(dict.fromkeys(labelList))
def nodify(node_names):
node_names = sankey_node
# 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=sankey_node)
fig = go.Figure(data=[go.Sankey( node = dict( pad=15,thickness=15,line = dict(color = "black", width = 0.5),label = labelList,color = 'grey',x=nodified[0],y=nodified[1] ),
link = dict(source = dataset.source.apply(lambda x: labelList.index(x)),
target = dataset.target.apply(lambda x: labelList.index(x)),
value = dataset.value),
arrangement='snap')])
fig.update_layout(title="performance Goal user behavior monitor",margin=dict(l=150))
fig.write_html('perfUXRGoal.html', auto_open=True)
问题: 标签位于节点的右侧,但最后一层的节点标签始终位于节点的左侧。
问题: 是否可以将标签也放在最后一层的节点右侧?
截图示例:
解决方法。有一个没有标签的附加层,所有目标节点都指向它。
import pandas as pd
import numpy as np
import plotly.graph_objects as go
links = [
{'source': 'start', 'target': 'A', 'value': 2},
{'source': 'A', 'target': 'B', 'value': 1},
{'source': 'A', 'target':'C', 'value':.5},
{'source': 'B', 'target':'', 'value':10**-10},
{'source': 'C', 'target':'', 'value':10**-10},
]
df = pd.DataFrame(links)
nodes = np.unique(df[["source","target"]], axis=None)
nodes = pd.Series(index=nodes, data=range(len(nodes)))
fig = go.Figure(
go.Sankey(
node={"label": nodes.index},
link={
"source": nodes.loc[df["source"]],
"target": nodes.loc[df["target"]],
"value": df["value"],
"color": np.where(df["target"].eq(""), "white", "rgba(30,30,30,.3)")
},
)
)
fig