Plotly Sankey 图组标签和颜色 [英] Plotly Sankey diagram group label and color

查看:153
本文介绍了Plotly Sankey 图组标签和颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 plotly 创建一个桑基图,并且有使用组"来组合节点的内置方法.但是,当我使用它时,该节点的颜色将为黑色,并且不显示标签.这是预期的,因为分组节点的颜色可能会有所不同.但是,我不知道如何设置组的颜色.标签也是如此.有没有办法定义这个?

示例代码:

import plotly.graph_objs as go从 plotly.offline 导入图值 = [3,5,2,4,6]来源 = [0,0,1,0,3]目标 = [1,4,2,3,4]color = ["蓝色","黄色","橙色","橙色","紫色"]标签 = ["A","B","C1","C2","D"]数据 = 字典(类型='桑基',安排 = '自由形式',节点 = 字典(垫 = 15,厚度 = 20,行 = 字典(颜色 = "黑色",宽度 = 0.1),组 = [[2,3]],标签 = 标签,颜色 = 颜色,),链接 = 字典(来源=来源,目标 = 目标,价值 = 价值,))布局 = 字典(title = "桑基测试",字体 = 字典(大小 = 10))f = go.FigureWidget(data=[data], layout=layout)情节(f)

呈现:

解决方案

因为我在您的代码段中遇到以下错误:

<块引用>

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

而且由于我不知道您正在运行什么版本的 plotly、python(和 Jupyter Notebook?),我只是建议您重构源数据并将 C1 和 C2 分组为简单的 C,然后再构建您的阴谋.请记住,

代码:

# 导入将熊猫导入为 pd将 numpy 导入为 np导入 plotly.graph_objs as gofrom plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot# 设置init_notebook_mode(连接=真)# 节点 &链接节点 = [['ID', '标签', '颜色'],[0,'A','蓝色'],[1,'B','黄色'],[2,'C','橙色'],[3,'D','紫色'],]# 与您的数据的链接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)'],]# 检索标题并构建数据帧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)# 桑基图设置数据跟踪 = 字典(类型='桑基',域 = 字典(x = [0,1],y = [0,1]),方向 = "h",valueformat = ".0f",节点 = 字典(垫 = 10,# 厚度 = 30,行 = 字典(颜色 = "黑色",宽度 = 0),label = df_nodes['Label'].dropna(axis=0, how='any'),颜色 = df_nodes['颜色']),链接 = 字典(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'),))布局 = 字典(title = "桑基测试",高度 = 772,字体 = 字典(大小 = 10),)fig = dict(data=[data_trace], layout=layout)iplot(无花果,验证=假)

我的系统信息:

笔记本服务器版本为:5.6.0服务器在此版本的 Python 上运行:Python 3.7.0(默认,2018 年 6 月 28 日,08:04:48)[MSC v.1912 64 位 (AMD64)]

I'm creating a sankey diagram using plotly and there is the built in method to use 'group' to combine nodes. However, when I use this the color of this node will be black and no label is showing. This is expected as the colors of the grouped nodes could vary. However, I don't see how I can set the color of the group. Same goes for the label. Is there a way to define this?

example code:

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)

Which renders:

解决方案

Since I'm getting the following error with your snippet:

ValueError: Invalid property specified for object of type plotly.graph_objs.sankey.Node: 'groups'

And since I don't know what versions you are running of plotly, python (and Jupyter Notebook?), I would simply suggest that you restructure your source data and do the C1 and C2 grouping into simply C before you build your plot. And keep in mind that Links are assigned in the order they appear in dataset and that node colors are assigned in the order that the plot is built.

Plot:

Code:

# 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)

My system info:

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)]

这篇关于Plotly Sankey 图组标签和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆