簡體   English   中英

如何以編程方式在 Plotly 中定義 sankey 標簽

[英]How to programmatically define sankey labels in Plotly

因此,我創建了一種以編程方式定義 plotly 中的桑基圖的源、目標和值列表的方法,從字典列表開始。 因此,如果您正在尋找一種方法來做到這一點,那就是。

但是,我堅持想辦法用類似的方法定義標簽。

任何幫助表示贊賞。

my_data = [
{'src':'wages','dst':'budget', 'value':1500},
{'src':'other','dst':'budget', 'value':250},
{'src':'budget','dst':'taxes', 'value':450},
{'src':'budget','dst':'housing', 'value':420},
{'src':'budget','dst':'food', 'value':400},
{'src':'budget','dst':'transportation', 'value':295},
{'src':'budget','dst':'savings', 'value':25},
{'src':'budget','dst':'other necessities', 'value':160},
]

i = 0 
node_names = []
my_data2 = []
for row in my_data:
    key_src = row['src']
    if (key_src not in node_names):
        node_names.append(key_src)
        i = i + 1
    row['src_id'] = i
    my_data2.append(row)

for row in my_data:
    key_dst = row['dst']
    if (key_dst not in node_names):
        node_names.append(key_dst)
        i = i + 1
    row['dst_id'] = i
    my_data2.append(row)
    
del node_names 

my_data2 = [dict(t) for t in {tuple(d.items()) for d in my_data2}] # Remove duplicates 


source = []
target = []
value = []

for row in my_data2:
    source.append(row['src_id'])
    target.append(row['dst_id'])
    value.append(row['value'])
    

print(source)
print(target)
print(value)


import plotly.graph_objects as go

link = dict(source = source, target = target, value = value)
data = go.Sankey(link = link)


# data
label = ["ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE"]
# data to dict, dict to sankey
link = dict(source = source, target = target, value = value)
node = dict(label = label, pad=50, thickness=5)
data = go.Sankey(link = link, node=node)
# plot
fig = go.Figure(data)
fig.show()

這可能是限制您的數據的一種方式。 我們提出了使用原始字典格式數據作為數據框來創建標簽的想法。 您將獲得起點的唯一字符串列表和終點的唯一字符串列表,並將這些列表連接在一起。 重疊的字符串是中心點的 label。 我們使用 set() 來解決這個重復並仍然保持原始列表順序。 最后,在開頭插入一個空字符串。

import pandas as pd

df = pd.DataFrame.from_dict(my_data)
df

    src     dst     value   src_id  dst_id
0   wages   budget  1500    1   3
1   other   budget  250     2   3
2   budget  taxes   450     3   4
3   budget  housing     420     3   5
4   budget  food    400     3   6
5   budget  transportation  295     3   7
6   budget  savings     25  3   8
7   budget  other necessities   160     3   9


src_dst = list(df['src'].unique()) + list(df['dst'].unique())
labels = sorted(set(src_dst), key=src.index)
labels.insert(0,'')

labels
['',
 'wages',
 'other',
 'budget',
 'taxes',
 'housing',
 'food',
 'transportation',
 'savings',
 'other necessities']

import plotly.graph_objects as go

link = dict(source = source, target = target, value = value)
data = go.Sankey(link = link)
    
# data
#label = ["ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE"]
label = labels
# data to dict, dict to sankey
link = dict(source = source, target = target, value = value)
node = dict(label = label, pad=50, thickness=5)
data = go.Sankey(link = link, node=node)
# plot
fig = go.Figure(data)
fig.show()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM