[英]plotly sankey graph data formatting
plotly 圖書館有一些不錯的桑基圖https://plotly.com/python/sankey-diagram/
但數據要求您傳遞源/目標對的索引。
link = dict(
source = [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A1, B1, ...
target = [2, 3, 3, 4, 4, 5],
我想知道是否有一個 API 可以簡單地傳遞這些對的命名列表?
links = [
{'source': 'start', 'target': 'A', 'value': 2},
{'source': 'A', 'target': 'B', 'value': 2},
...
]
這更符合bokeh/holoviews期望數據的方式(但 sankey 不適用於自循環)
還有這個pysankey小部件
所以我可以在不處理所有內容的情況下將 map 靠近我的 dataframe 嗎?
或者,是否有一種很好的 pythonic 方式將其轉換為一個班輪:D
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}
]
df = pd.DataFrame(links)
nodes = np.unique(df[["source","target"]], axis=None)
nodes = pd.Series(index=nodes, data=range(len(nodes)))
go.Figure(
go.Sankey(
node={"label": nodes.index},
link={
"source": nodes.loc[df["source"]],
"target": nodes.loc[df["target"]],
"value": df["value"],
},
)
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.