我想将节点计数添加到Plotly Sankey图(https://plot.ly/python/sankey-diagram/)中的每个节点,以使其看起来像红色箭头引用的计数。

这个是可能的吗?我在plotly中找不到这样的例子。上面提供的示例来自R (https://github.com/fbreitwieser/sankeyD3/blob/master/README.md)中的一个库,但我使用的是Python语言。
下面是我的代码。
import plotly.graph_objects as go
import pandas as pd
def plot_sankey(df, title):
# Get column names
cat_columns = [key for key, value in df.dtypes.iteritems() if value == 'O']
# Mapping to unique values for categorical columns
labels = pd.unique(df[cat_columns].values.ravel('K')).tolist()
# Break dowmn each step
final = []
for i, row in df.iterrows():
cat_values = row[cat_columns].values
value = row['value']
final.extend([(a, b, value) for a, b in zip(cat_values, cat_values[1:]) if a!=None and b!=None])
# Build formatted version
df_formatted = pd.DataFrame(final, columns=['source', 'target', 'value'])
# Build Node
node = dict(
pad = 15,
thickness = 20,
line = dict(color = "black", width = 0.5),
label = labels,
color = "blue"
)
# Build Link
link = dict(
source = [labels.index(x) for x in df_formatted['source'].tolist()],
target = [labels.index(x) for x in df_formatted['target'].tolist()],
value = df_formatted['value'].tolist()
)
# Plot
fig = go.Figure(data=[go.Sankey(node=node, link=link, visible=True)])
fig.update_layout(title_text=title,
font_size=10,
autosize=False,
width=2750,
height=1600)
fig.show()发布于 2020-09-03 01:05:55
这似乎不像是直接可能的。但是,您通过以下方式提供的任何
node = dict( label = ["A1", "A2", "B1", "B2", "C1"])将显示在绘图上。
假设您可以预先计算每个节点的总数,您可以为每个节点传递一个字符串,其中的名称和值如下所示:
label = ["{} {}".format(node1_name, node1_val), "{} {}".format(node2_name, node2_val) ...]或者,您可以在悬停状态下执行此操作。参见here。
plotly sunburst允许你通过"textinfo“属性很好地做到这一点,你只需要传递"label+value”--但这在plotly 4.9 docs的sankey中是不可用的。
https://stackoverflow.com/questions/59975614
复制相似问题