Plotly Sankey图组标签和颜色

问题描述 投票:1回答:1

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

示例代码:

import plotly.graph_objs as go
from plotly.offline import plot

value = [3,5,2,4,6]
source = [0,0,1,0,3]
target = [1,4,2,3,4]
color = ["blue","yellow","orange","orange","purple"]
label = ["A","B","C1","C2","D"]


data = dict(
    type='sankey',
    arrangement = 'freeform',
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(
        color = "black",
        width = 0.1
      ),
      groups = [[2,3]],
      label = label,
      color = color,
    ),
    link = dict(
        source = source,
        target = target,
        value = value,
      )
)

layout =  dict(
    title = "Sankey test",
    font = dict(
      size = 10
    )
)
f = go.FigureWidget(data=[data], layout=layout)
plot(f)

哪个呈现:

python plotly sankey-diagram
1个回答
0
投票

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

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

既然我不知道你运行的是什么版本的图形,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)]
© www.soinside.com 2019 - 2024. All rights reserved.