有没有办法格式化绘制Sankey图显示?

发布于 2025-01-19 00:48:38 字数 2364 浏览 2 评论 0原文

我正在创建一个桑基图,如下所示:

import plotly.graph_objects as go

fig = go.Figure(data=[go.Sankey(
        valueformat = ".0f",
        valuesuffix = " %",
        orientation = "h",
        node = dict(
          pad = 20,
          thickness = 20,
          line = dict(color = "red", width = 1),
          label = ['Equity',
                   'Global Equity',
                   'Tier 1',
                   'A looooooooooong',
                   'Tier 2',
                   'B looooooooooong',
                   'C looooooooooong',
                   'Tier 3',
                   'D looooooooooong',
                   'E looooooooooong',
                   'F looooooooooong',
                   'G looooooooooong',
                   'H looooooooooong'],
          color = ['aqua',
                 'aqua',
                 'yellow',
                 'orange',
                 'yellow',
                 'orange',
                 'orange',
                 'yellow',
                 'orange',
                 'orange',
                 'orange',
                 'orange',
                 'orange'],
        ),
        link = dict(
          source = [0, 2, 1, 4, 4, 2, 7, 7, 7, 7, 7, 4],
          target = [1, 3, 2, 5, 6, 4, 8, 9, 10, 11, 12, 7],
          value = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
          color = ['aqua',
                 'yellow',
                 'aqua',
                 'yellow',
                 'yellow',
                 'aqua',
                 'yellow',
                 'yellow',
                 'yellow',
                 'yellow',
                 'yellow',
                 'aqua'],
            hovertemplate='This link has total value %{value}<extra></extra>'
      ))])
    
    fig.update_layout(title_text="Waterfall Diagram", 
                      font_size=16,
                      plot_bgcolor='white',
                      paper_bgcolor='white')
    fig.show()

输出如下所示:

在此处输入图像描述

有没有办法:

  1. 确保浅绿色的链接始终位于黄色链接的下方?为了在视觉上更好地将它们分开 - 我不确定为什么当前的设置以这种顺序显示它们,
  2. 以便在链接之间留出更多空间,从而将它们分散得更多?特别是我需要链接和节点不相互重叠,
  3. 进一步展开水绿色链接?即在视觉上将它们与其他节点分离
  4. 以控制节点标签的显示位置和方式?即到右侧或下方节点,并控制每个节点的字体

I am creating a Sankey diagram with plotly as follows:

import plotly.graph_objects as go

fig = go.Figure(data=[go.Sankey(
        valueformat = ".0f",
        valuesuffix = " %",
        orientation = "h",
        node = dict(
          pad = 20,
          thickness = 20,
          line = dict(color = "red", width = 1),
          label = ['Equity',
                   'Global Equity',
                   'Tier 1',
                   'A looooooooooong',
                   'Tier 2',
                   'B looooooooooong',
                   'C looooooooooong',
                   'Tier 3',
                   'D looooooooooong',
                   'E looooooooooong',
                   'F looooooooooong',
                   'G looooooooooong',
                   'H looooooooooong'],
          color = ['aqua',
                 'aqua',
                 'yellow',
                 'orange',
                 'yellow',
                 'orange',
                 'orange',
                 'yellow',
                 'orange',
                 'orange',
                 'orange',
                 'orange',
                 'orange'],
        ),
        link = dict(
          source = [0, 2, 1, 4, 4, 2, 7, 7, 7, 7, 7, 4],
          target = [1, 3, 2, 5, 6, 4, 8, 9, 10, 11, 12, 7],
          value = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
          color = ['aqua',
                 'yellow',
                 'aqua',
                 'yellow',
                 'yellow',
                 'aqua',
                 'yellow',
                 'yellow',
                 'yellow',
                 'yellow',
                 'yellow',
                 'aqua'],
            hovertemplate='This link has total value %{value}<extra></extra>'
      ))])
    
    fig.update_layout(title_text="Waterfall Diagram", 
                      font_size=16,
                      plot_bgcolor='white',
                      paper_bgcolor='white')
    fig.show()

Output looks like this:

enter image description here

Is there a way:

  1. to make sure links in color aqua are always below the yellow ones? to visually separate them better - I am not sure why current set up show them in that order
  2. to give more space between the links, spreading them out more? Especially I would need links and nodes not to overlap each other
  3. spread out the aqua links even further? I.e. visually dissociate them from the others
  4. to control where and how node labels are shown? I.e. to the right or below node, and also controlling the font for each node

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桜花祭 2025-01-26 00:48:38

对于 1, 2, & 3,您可以按如下方式显式设置节点位置:

import plotly.graph_objects as go

fig = go.Figure(go.Sankey(
    arrangement = "snap",
    node = {
        "label": ["A", "B", "C", "D", "E", "F"],
        "x": [0.2, 0.1, 0.5, 0.7, 0.3, 0.5], #these are fractions of the domain (0,1)
        "y": [0.7, 0.5, 0.2, 0.4, 0.2, 0.3],
        'pad':10},  # 10 Pixels
    link = {
        "source": [0, 0, 1, 2, 5, 4, 3, 5],
        "target": [5, 3, 4, 3, 0, 2, 2, 3],
        "value": [1, 2, 1, 1, 1, 1, 1, 2]}))

fig.show()

来自plotly 文档的示例。

For 1, 2, & 3, you can set node locations explicitly as follows:

import plotly.graph_objects as go

fig = go.Figure(go.Sankey(
    arrangement = "snap",
    node = {
        "label": ["A", "B", "C", "D", "E", "F"],
        "x": [0.2, 0.1, 0.5, 0.7, 0.3, 0.5], #these are fractions of the domain (0,1)
        "y": [0.7, 0.5, 0.2, 0.4, 0.2, 0.3],
        'pad':10},  # 10 Pixels
    link = {
        "source": [0, 0, 1, 2, 5, 4, 3, 5],
        "target": [5, 3, 4, 3, 0, 2, 2, 3],
        "value": [1, 2, 1, 1, 1, 1, 1, 2]}))

fig.show()

Example from the plotly docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文