捆绑名称 Sankey plotly

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

我想知道如何在 plotly 中向 Sankey plot 添加 bundle 名称。我理解的 bundle 是指一列中的节点。从plotly教程中。

import plotly.graph_objects as go

fig = go.Figure(data=[go.Sankey(
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = ["A1", "A2", "B1", "B2", "C1", "C2"],
      color = "blue"
    ),
    link = dict(
      source = [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A2, B1, ...
      target = [2, 3, 3, 4, 4, 5],
      value = [8, 4, 2, 8, 4, 2]
  ))])

fig.update_layout(title_text="Basic Sankey Diagram", font_size=10)
fig.show()

enter image description here

我想添加的是捆绑名称,比如:enter image description here

我在文档中找不到这样的选项,也找不到破解它的方法。

plotly plotly-python
1个回答
1
投票

你觉得这可以作为一个起点吗?

fig = go.Figure()
fig.add_trace(go.Sankey(
    node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = ["A1", "A2", "B1", "B2", "C1", "C2"],
      color = "blue"
    ),
    link = dict(
      source = [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A2, B1, ...
      target = [2, 3, 3, 4, 4, 5],
      value = [8, 4, 2, 8, 4, 2]
  )))
fig.add_trace(go.Scatter(x=[0,1,2], y=[None]*3))
fig.update_xaxes(side='top')
fig.update_layout(title_text="Basic Sankey Diagram",
                  title_x=0.5,
                  font_size=10,
                  plot_bgcolor='white',
                  xaxis = dict(showgrid=False,
                               tickmode = 'array',
                               tickvals = [0, 1, 2],
                               ticktext = ["bundle name 1", "bundle name 2", "bundle name 3"]),
                  yaxis = dict(showgrid=False,
                               showticklabels=False)                             

                                    )

fig.show()

enter image description here

我用的技巧是

  • 添加一个假的散点 (yNone)
  • 移动 xaxes 至上
  • 将背景色改为 white
  • 摆脱 showgrid 双轴
  • 增加一个自定义ticks xaxis
  • 移除标签上的tickslabels yaxis
© www.soinside.com 2019 - 2024. All rights reserved.