" Sankey"的问题图

时间:2017-10-04 16:14:02

标签: python plotly sankey-diagram

我一直在努力为作业制作一个sankey图。我在https://plot.ly/~alishobeiri/1591/plotly-sankey-diagrams/找到了一个很棒的代码,我正在使用它并适应我的要求。但是我收到了错误:

  

SyntaxError:解析时的意外EOF

适用于所有dict ()

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

代码:

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)

1 个答案:

答案 0 :(得分:0)

以下是我解决它的方法:

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('')`