ricklupton / ipysankeywidget

IPython / Jupyter Sankey diagram widget
MIT License
174 stars 24 forks source link

graph stops drawing sometimes #72

Closed dcsan closed 1 year ago

dcsan commented 2 years ago

I'm often working with a sankey and sometimes it will just refuse to draw:

image

i can mess around with the params and eventually it will draw again - eg with 21 connections instead of 20. It seems to somehow give the renderer a kick.

This looks to me like the CSS isn't loading or something? I can only guess there's some loading order issue to the widget? Are you doing a server-side render of the whole D3 graph?

In this case when I save a png it also comes out the same.

ricklupton commented 2 years ago

Is it possible there is a NaN or other funny value in your data for the flow that is causing the problem when you add it back in?

On 6 Oct 2021, at 10:57, DC @.***> wrote:

 I'm often working with a sankey and sometimes it will just refuse to draw:

i can mess around with the params and eventually it will draw again - eg with 21 connections instead of 20. It seems to somehow give the renderer a kick.

This looks to me like the CSS isn't loading or something? I can only guess there's some loading order issue to the widget? Are you doing a server-side render of the whole D3 graph?

In this case when I save a png it also comes out the same.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

aluchuk commented 1 year ago

Solution I had the wrong column names, needs to be "value" I have "amount".

~~I'm also getting the same error. ~~ here is my code (example from https://github.com/IKNL/oncoguide_sankey/blob/master/scripts/oncoguide_sankey.ipynb)

import os
import pathlib
import pandas as pd

from ipysankeywidget import SankeyWidget

os.chdir(os.getcwd())

PATH_DATA = pathlib.Path(r'./storage/shared/Documents')
PATH_RESULTS = pathlib.Path(r'./storage/shared/Documents')

if not PATH_DATA.exists():
    raise ValueError("Data directory not found.")

df = pd.read_csv(PATH_DATA/'flow.csv', low_memory=False)

SankeyWidget(links=df.to_dict('records'))

the csv file:

source,target,amount
wages,income,75000
interest,income,30
other,income,5000
income,expenses,5
income,liabilities,6000
income,loans,300
income,savings,200
expenses,Education,1000
expenses,Insurance,1999
expenses,Medical,123
expenses,utilities,240
liabilities,Chase,745
liabilities,Discover,137
loans,Education,2500
loans,Mortgage,17000
loans,Car,6000

and the result sankey_simple

P.S. to_dict seems to have the right layout:

df.to_dict('records')
[{'source': 'wages', 'target': 'income', 'amount': 75000},
 {'source': 'interest', 'target': 'income', 'amount': 30},
 {'source': 'other', 'target': 'income', 'amount': 5000},
 {'source': 'income', 'target': 'expenses', 'amount': 5},
 {'source': 'income', 'target': 'liabilities', 'amount': 6000},
 {'source': 'income', 'target': 'loans', 'amount': 300},
 {'source': 'income', 'target': 'savings', 'amount': 200},
 {'source': 'expenses', 'target': 'Education', 'amount': 1000},
 {'source': 'expenses', 'target': 'Insurance', 'amount': 1999},
 {'source': 'expenses', 'target': 'Medical', 'amount': 123},
 {'source': 'expenses', 'target': 'utilities', 'amount': 240},
 {'source': 'liabilities', 'target': 'Chase', 'amount': 745},
 {'source': 'liabilities', 'target': 'Discover', 'amount': 137},
 {'source': 'loans', 'target': 'Education', 'amount': 2500},
 {'source': 'loans', 'target': 'Mortgage', 'amount': 17000},
 {'source': 'loans', 'target': 'Car', 'amount': 6000}]