[英]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.