user3139545
user3139545

Reputation: 7374

Sankey diagram in Plotly not showing any graph

Im trying to get a simple example going of a Sankey diagram in plotly. The code below shows what im trying to do.

import pandas as pd
df = pd.DataFrame()
df['source'] = ['s1', 's2', 's3']
df['target'] = ['s2', 's3', 's4']
df['value'] = [2,2,1]
df['label'] = ['a','b','c']
df['color'] = ['rgba(31, 119, 180, 0.8)', 'rgba(31, 119, 180, 0.8)', 'rgba(31, 119, 180, 0.8)'] 'rgba(31, 119, 180, 0.8)']

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from plotly.graph_objs import *
init_notebook_mode(connected=True)

trace1 = {
  "domain": {
     "x": [0, 1], 
     "y": [0, 1]
  }, 
"link": dict({
  #"label": ['stream 1', '', '', ''], 
  "source": df['source'].dropna(axis=0, how='any'),
  "target": df['target'].dropna(axis=0, how='any'),
  "value": df['value'].dropna(axis=0, how='any'),
  "color": df['color'].dropna(axis=0, how='any')
 }), 
"node": dict({
  "color": df['color'],
  "label": df['label'].dropna(axis=0, how='any'), 
  "line": {
     "color": "black", 
     "width": 0.5
 }, 
  "pad": 0.5, 
  "thickness": 15
  }), 
 "orientation": "h", 
 "type": "sankey", 
 "valueformat": ".0f", 
 "valuesuffix": "Ha"
}
data = Data([trace1])
layout = {
   "font": {"size": 10}, 

 }
fig = Figure(data=data, layout=layout)

iplot(fig, validate=False)

However when I run this inside ipython notebook nothing shows. What am i missing?

Upvotes: 3

Views: 4030

Answers (1)

gfhuertac
gfhuertac

Reputation: 366

Keep in mind that source and target in sankey's diagram must be numeric and not text. In your case it is better to create separate objects for both links and nodes. Here is an example based on your data:

import pandas as pd
df_links = pd.DataFrame()
df_links['source'] = [0, 1, 2]
df_links['target'] = [1, 2, 3]
df_links['value'] = [2, 2, 1]
df_links['color'] = ['rgba(31, 119, 180, 0.8)', 'rgba(31, 119, 180, 0.8)', 'rgba(31, 119, 180, 0.8)']
df_nodes = pd.DataFrame()
df_nodes['label'] = ['s1','s2','s3','s4']
df_nodes['color'] = ['rgba(31, 119, 180, 0.8)', 'rgba(31, 119, 180, 0.8)', 'rgba(31, 119, 180, 0.8)', 'rgba(31, 119, 180, 0.8)']
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
from plotly.graph_objs import *

init_notebook_mode(connected=True)

trace1 = {
  "domain": {
    "x": [0, 1], 
    "y": [0, 1]
  }, 
  "link": dict({
    #"label": ['stream 1', '', '', ''], 
    "source": df['source'].dropna(axis=0, how='any'),
    "target": df['target'].dropna(axis=0, how='any'),
    "value": df['value'].dropna(axis=0, how='any'),
    "color": df['color'].dropna(axis=0, how='any')
  }), 
  "node": dict({
    "color": df['color'].dropna(axis=0, how='any'),
    "label": df['label'].dropna(axis=0, how='any'), 
    "line": {
      "color": "black", 
      "width": 0.5
    }, 
    "pad": 0.5, 
    "thickness": 15
  }), 
  "orientation": "h", 
  "type": "sankey", 
  "valueformat": ".0f", 
  "valuesuffix": "Ha"
}

layout = {
   "font": {"size": 10}, 
}

fig = Figure(data=[trace1], layout=layout)
iplot(fig, validate=False)

Please note that some online service block inline code for security, so test it on your own computer first.

Upvotes: 5

Related Questions