Skip to content

Instantly share code, notes, and snippets.

@mebaysan
Created January 28, 2021 15:27
Show Gist options
  • Save mebaysan/ab4be429612b4a2e2065dd5e43cea0d5 to your computer and use it in GitHub Desktop.
Save mebaysan/ab4be429612b4a2e2065dd5e43cea0d5 to your computer and use it in GitHub Desktop.
Verilen Pathe Göre Otomatik Sankey Diagramı Verisi Hazırlayan Fonksiyon
import plotly.graph_objects as go
def get_sankey(data,path,value_col):
sankey_data = {
'label':[],
'source': [],
'target' : [],
'value' : []
}
counter = 0
while (counter < len(path) - 1):
for parent in data[path[counter]].unique():
sankey_data['label'].append(parent)
for sub in data[data[path[counter]] == parent][path[counter+1]].unique():
sankey_data['source'].append(sankey_data['label'].index(parent))
sankey_data['label'].append(sub)
sankey_data['target'].append(sankey_data['label'].index(sub))
sankey_data['value'].append(data[data[path[counter+1]] == sub][value_col].sum())
counter +=1
return sankey_data
my_sankey = get_sankey(sankey_df,['Kategori','Projeli_mi','Ürün Adı'],'Miktar')
fig = go.Figure(data=[go.Sankey(
node = dict(
pad = 15,
thickness = 20,
line = dict(color = "black", width = 0.5),
label = my_sankey['label'],
color = "blue"
),
link = dict(
source = my_sankey['source'],
target = my_sankey['target'],
value = my_sankey['value']
))])
fig.write_html('test.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment