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

这可能吗?我在情节中找不到这样的例子。我上面提供的示例来自 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()
Run Code Online (Sandbox Code Playgroud)
直接看好像不太可能。但是,您通过提供的任何内容
node = dict( label = ["A1", "A2", "B1", "B2", "C1"])
Run Code Online (Sandbox Code Playgroud)
将显示在绘图上。
假设您可以预先计算每个节点的总数,您可以为每个节点传递一个具有名称和值的字符串,如下所示:
label = ["{} {}".format(node1_name, node1_val), "{} {}".format(node2_name, node2_val) ...]
Run Code Online (Sandbox Code Playgroud)
或者,您可以在悬停时执行此操作。看这里。
plotly sunburst 允许您通过“textinfo”属性很好地做到这一点,您可以在其中传递“label+value” - 但这在 sankey 中不可用,从plotly 4.9 docs开始。
| 归档时间: |
|
| 查看次数: |
8038 次 |
| 最近记录: |