def sankey(df, filename=None):
    'SanKey图绘制\n    df的列是左节点，行是右节点\n    注:暂时没找到好的Python方法，所以只生成R语言所需数据\n    返回links 和 nodes\n    # R code 参考\n    library(networkD3)\n    dd=read.csv(\'price_links.csv\')\n    links<-data.frame(source=dd$from,target=dd$to,value=dd$value)\n    nodes=read.csv(\'price_nodes.csv\',encoding = \'UTF-8\')\n    nodes<-nodes[\'name\']\n    Energy=c(links=links,nodes=nodes)\n    sankeyNetwork(Links = links, Nodes = nodes, Source = "source",\n                  Target = "target", Value = "value", NodeID = "name",\n                  units = "TWh",fontSize = 20,fontFamily=\'微软雅黑\',nodeWidth=20)\n    '
    nodes = ['Total']
    nodes = ((nodes + list(df.columns)) + list(df.index))
    nodes = pd.DataFrame(nodes)
    nodes['id'] = range(len(nodes))
    nodes.columns = ['name', 'id']
    (R, C) = df.shape
    t1 = pd.DataFrame(df.as_matrix(), columns=range(1, (C + 1)), index=range((C + 1), ((R + C) + 1)))
    t1.index.name = 'to'
    t1.columns.name = 'from'
    links = t1.unstack().reset_index(name='value')
    links0 = pd.DataFrame({'from': ([0] * C), 'to': range(1, (C + 1)), 'value': list(df.sum())})
    links = links0.append(links)
    if filename:
        links.to_csv((filename + '_links.csv'), index=False, encoding='utf-8')
        nodes.to_csv((filename + '_nodes.csv'), index=False, encoding='utf-8')
    return (links, nodes)
SanKey图绘制
df的列是左节点，行是右节点
注:暂时没找到好的Python方法，所以只生成R语言所需数据
返回links 和 nodes
# R code 参考
library(networkD3)
dd=read.csv('price_links.csv')
links<-data.frame(source=dd$from,target=dd$to,value=dd$value)
nodes=read.csv('price_nodes.csv',encoding = 'UTF-8')
nodes<-nodes['name']
Energy=c(links=links,nodes=nodes)
sankeyNetwork(Links = links, Nodes = nodes, Source = "source",
              Target = "target", Value = "value", NodeID = "name",
              units = "TWh",fontSize = 20,fontFamily='微软雅黑',nodeWidth=20)
def sankey(df, filename=None):
    'SanKey图绘制\n    df的列是左节点，行是右节点\n    注:暂时没找到好的Python方法，所以只生成R语言所需数据\n    返回links 和 nodes\n    # R code 参考\n    library(networkD3)\n    dd=read.csv(\'price_links.csv\')\n    links<-data.frame(source=dd$from,target=dd$to,value=dd$value)\n    nodes=read.csv(\'price_nodes.csv\',encoding = \'UTF-8\')\n    nodes<-nodes[\'name\']\n    Energy=c(links=links,nodes=nodes)\n    sankeyNetwork(Links = links, Nodes = nodes, Source = "source",\n                  Target = "target", Value = "value", NodeID = "name",\n                  units = "TWh",fontSize = 20,fontFamily=\'微软雅黑\',nodeWidth=20)\n    '
    nodes = ['Total']
    nodes = ((nodes + list(df.columns)) + list(df.index))
    nodes = pd.DataFrame(nodes)
    nodes['id'] = range(len(nodes))
    nodes.columns = ['name', 'id']
    (R, C) = df.shape
    t1 = pd.DataFrame(df.as_matrix(), columns=range(1, (C + 1)), index=range((C + 1), ((R + C) + 1)))
    t1.index.name = 'to'
    t1.columns.name = 'from'
    links = t1.unstack().reset_index(name='value')
    links0 = pd.DataFrame({'from': ([0] * C), 'to': range(1, (C + 1)), 'value': list(df.sum())})
    links = links0.append(links)
    if filename:
        links.to_csv((filename + '_links.csv'), index=False, encoding='utf-8')
        nodes.to_csv((filename + '_nodes.csv'), index=False, encoding='utf-8')
    return (links, nodes)