散布Sankey图组标签和颜色

时间:2019-04-26 06:33:08

标签: python plotly sankey-diagram

我正在使用plotly创建一个sankey图,并且有一种内置方法可以使用“ group”组合节点。但是,当我使用此颜色时,此节点的颜色将为黑色,并且未显示任何标签。这是预期的,因为分组节点的颜色可能会有所不同。但是,我看不到如何设置组的颜色。标签也一样。 有没有办法定义这个?

示例代码:

<asp:TextBox runat="server" ID="txtValue" ClientIDMode="Static" />
<asp:RequiredFieldValidator ID="_validatorForExeFile" runat="server" ControlToValidate="txtValue">
<asp:Image ID="_imgExeFolderName" runat="server" ToolTip="Main executable missing." ImageUrl="~/error.png"                                       meta:resourcekey="_actionExeFolderIconResource" />
</asp:RequiredFieldValidator>

哪个渲染: Taskrouter Workflow documentation

1 个答案:

答案 0 :(得分:0)

由于我的代码段出现以下错误:

  

ValueError:为plotly.graph_objs.sankey.Node类型的对象指定了无效的属性:'groups'

而且由于我不知道您正在运行的是什么版本的python,Python(和Jupyter Notebook?),我只是建议您在构建自己的源数据之前,将C1和C2分组为简单的C情节。并请记住,Links are assigned in the order they appear in dataset和节点颜色是按照绘制图的顺序分配的。

情节:

enter image description here

代码:

# imports
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

# settings
init_notebook_mode(connected=True)

# Nodes & links
nodes = [['ID', 'Label', 'Color'],
        [0,'A','blue'],
        [1,'B','yellow'],
        [2,'C','orange'],
        [3,'D','purple'],
        ]

# links with your data
links = [['Source','Target','Value','Link Color'],
        [0,1,3,'rgba(200, 205, 206, 0.6)'],
        [0,2,5,'rgba(200, 205, 206, 0.6)'],
        [0,3,5,'rgba(200, 205, 206, 0.6)'],
        [1,2,6,'rgba(200, 205, 206, 0.6)'],
        [2,3,6,'rgba(200, 205, 206, 0.6)'],
        ]

# Retrieve headers and build dataframes
nodes_headers = nodes.pop(0)
links_headers = links.pop(0)
df_nodes = pd.DataFrame(nodes, columns = nodes_headers)
df_links = pd.DataFrame(links, columns = links_headers)

# Sankey plot setup
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
      ),
      label =  df_nodes['Label'].dropna(axis=0, how='any'),
      color = df_nodes['Color']
    ),
    link = dict(
      source = df_links['Source'].dropna(axis=0, how='any'),
      target = df_links['Target'].dropna(axis=0, how='any'),
      value = df_links['Value'].dropna(axis=0, how='any'),
      color = df_links['Link Color'].dropna(axis=0, how='any'),
  )
)

layout = dict(
        title = "Sankey Test",
    height = 772,
    font = dict(
      size = 10),)

fig = dict(data=[data_trace], layout=layout)
iplot(fig, validate=False)

我的系统信息:

The version of the notebook server is: 5.6.0
The server is running on this version of Python:
Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)]