plotly - 捆绑名称 Sankeyplotly

标签 plotly plotly-python

我想知道如何将包名称添加到桑基图中。通过捆绑我了解一列中的节点。来自 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

我想添加的是 bundle 标题,例如: enter image description here

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

最佳答案

您认为这可以作为一个起点吗?

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移至顶部
  • 将背景颜色更改为白色
  • 去掉两个轴上的showgrid
  • xaxis上添加自定义刻度
  • 删除 y 轴上的刻度标签

关于plotly - 捆绑名称 Sankeyplotly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61460424/

相关文章:

javascript - 如何删除 plotly.js x 轴刻度标签/按钮?

Plotly:在同一个图中绘制直线和散点图

python - 如何从基于颜色的分组条中禁用 plotly express?

python - 如何在悬停模板中包含跟踪名称?

d3.js - plotly .ly : Simple prefix or suffix for hover text

python - 在Python中自定义散点图的气泡大小

python - Jupyter Notebook 中未显示 Plotly Sunburst 图

python - Plotly:如何将分类变量插入平行坐标图中?

python - Plotly:条形图不透明度随着时间范围的延长而变化

python - 绘制悬停数据和悬停文本