You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Python中Plotly Sankey图的轴标签移至图外?

问题描述

以下是我的代码:

def fig_sankey_creator(sankey_label, sankey_source, sankey_target, sankey_value, node_color, link_color):   


    fig_sankey = go.Figure(data = [go.Sankey(
                                node = dict(
                                label = sankey_label,
                                color = node_color,
                                pad = 10,
                            ),
                                link = dict(
                                        source = sankey_source,
                                        target = sankey_target,
                                        value = sankey_value,
                                        color = link_color,
                            ))])
    fig_sankey.update_layout(
            title_text = "Sankey diagram",
            height = 800,
            width = 1600,
            )
    fig_sankey.update_yaxes(ticklabelposition = "outside")
            
    return fig_sankey

当前生成的Sankey图效果如下:
Sankey图效果

我希望将轴标签从图内移至图外,但使用ticklabelposition = "outside"参数无法实现该需求,请问该如何解决?谢谢!


解决方法

update_yaxes的ticklabelposition对Sankey图无效,因为Sankey的节点标签不属于传统坐标轴刻度,而是组件自身的节点属性,得通过调整go.Sankey的node参数和布局边距来实现标签外移:

  1. 控制节点位置与标签对齐
    通过node.x设置节点在画布中的水平位置(0为最左,1为最右),再用node.align设置标签相对于节点的对齐方向,让标签自然落在图外;同时调大pad值,增加节点和标签的间距。

  2. 调整画布边距
    用margin参数给左右两侧留出足够空间,避免标签被画布截断。

修改后的代码示例:

def fig_sankey_creator(sankey_label, sankey_source, sankey_target, sankey_value, node_color, link_color):   
    # 根据节点分组设置水平位置,示例按左右两组划分,可按需调整
    node_x = [0 if i < len(sankey_label)//2 else 1 for i in range(len(sankey_label))]
    # 对应节点的标签对齐方式:左侧节点左对齐,右侧节点右对齐
    node_align = ['left' if x == 0 else 'right' for x in node_x]

    fig_sankey = go.Figure(data = [go.Sankey(
                                node = dict(
                                    label = sankey_label,
                                    color = node_color,
                                    pad = 20,  # 增大节点与标签的间距
                                    x = node_x,  # 控制节点水平位置
                                    align = node_align,  # 标签对齐方向
                                    thickness = 20
                                ),
                                link = dict(
                                        source = sankey_source,
                                        target = sankey_target,
                                        value = sankey_value,
                                        color = link_color,
                                ))])
    fig_sankey.update_layout(
            title_text = "Sankey diagram",
            height = 800,
            width = 1600,
            margin = dict(l=150, r=150, t=50, b=50)  # 增大左右边距容纳标签
            )
            
    return fig_sankey

关键说明

  • 如果是多层Sankey图,可以给不同层级的节点设置0, 0.33, 0.66, 1这类分段x值,配合对应方向的align,让每一层的标签都显示在外侧。
  • pad和margin的数值可以根据标签长度、画布大小灵活调整,确保标签完整显示且布局美观。

内容的提问来源于stack exchange,提问作者Mr.Slow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 16:20:37