2

I have been trying to make a sankey diagram for an assignment. I found an amazing code at: https://plot.ly/~alishobeiri/1591/plotly-sankey-diagrams/ which I am using and adapting to my requirements. However I am getting the error:

SyntaxError: unexpected EOF while parsing

for all dict ().

Data: https://drive.google.com/open?id=0BzIHBf19BxTbUktNcG1uSWtUOEE

Code:

import json, urllib
import plotly.plotly as py
import pandas as pd
import numpy as np

land_change = pd.read_csv(r'test.csv')
land_change.head()

data_trace = dict(
        type = 'sankey', 
        domain = dict(
                x =  [0,1], 
                y =  [0,1]), 
                orientation = "h",
                valueformat = ".0f",
                node = dict(
                        pad = 10,
                        thickness = 30,
                        line = dict(
                                color = "black",        
                                width = 0.5),
      label =  land_change['Node, Label'].dropna(axis=0, how='any'),
      color = land_change['Color']),
    link = dict(
      source = land_change['Source'].dropna(axis=0, how='any'),
      target = land_change['Target'].dropna(axis=0, how='any'),
      value = land_change['Value'].dropna(axis=0, how='any'),))

layout =  dict(
    title = "Land cover and use change for period 1990 - 2000 & 2000 - 2014.",
    height = 772,
    width = 950,
    font = dict(
      size = 10
    ),    
)

fig = dict(data=[data_trace], layout=layout)
py.iplot(fig, validate=False)
darthbith
  • 18,484
  • 9
  • 60
  • 76
Daniel
  • 218
  • 1
  • 2
  • 9
  • Can you add `test.csv` or some fake data to reproduce the issue? – Maximilian Peters Oct 04 '17 at 18:04
  • 2
    Your code would be vastly more understandable if you indented it in a meaningful manner. For example, `orientation` is indented as if it was part of the `domain` dict - but you actually finished that dict on the previous line. – jasonharper Oct 04 '17 at 19:28
  • This error is almost certainly because you're missing a close parentheses somewhere. As @jasonharper says, properly indenting your code will help immensely in finding the error. You might also try commenting out portions of it and seeing where the failure stops; then you can localize the error to that location. – darthbith Oct 04 '17 at 19:40

1 Answers1

0

Here is how I solved it:

import plotly as py2
import plotly.plotly as py
from plotly.graph_objs import *
import pandas as pd

py.sign_in('user', 'pass')

land_change = pd.read_csv(r'file')
land_change.head()

trace1 = {
  "domain": {
     "x": [0, 1], 
     "y": [0, 1]
  }, 
"link": dict({
  #"label": ['stream 1', '', '', ''], 
  "source": land_change ['Source'].dropna(axis=0, how='any'),
  "target": land_change ['Target'].dropna(axis=0, how='any'),
  "value": land_change ['Value'].dropna(axis=0, how='any'),
  " color": land_change ['Color'].dropna(axis=0, how='any')
 }), 
"node": dict({
  "color": land_change ['Node, Color'],
  "label": land_change ['Node, 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)
 #plot_url = py.plot(fig)
 py2.offline.plot(fig)
 py.image.save_as(fig, filename='')

 from IPython.display import Image
 Image('')`
Daniel
  • 218
  • 1
  • 2
  • 9