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

如何在Graphviz中高亮D到E的边?解决边无法重叠显示问题

Graphviz实现D-E边黄色高亮(解决边并排问题)

要在Graphviz的K5图中高亮D到E的边,尝试叠加额外边时,Graphviz未让边重叠,反而将它们并排显示。以下是原代码及异常效果:

原代码

graph K5 {
    graph [splines = false, layout=circo];
    edge [penwidth=3];
    node [shape=circle];
    A;
    B;
    C;
    D;
    E;
    {
        edge [color="red"];
        A -- B;
        A -- C;
        C -- D;
        B -- D;
        C -- E;
    }

    {
        edge [color="blue"];
        node [comment="Wildcard node added automatic in EG."];
        D -- E;
        A -- E;
        B -- C;
        B -- E;
        A -- D;
    }

    {
        edge [color="#95a616aa" penwidth=5];
        node [comment="Wildcard node added automatic in EG."];
        E -- D;
    }
}

异常效果

额外边并排显示,未重叠

解决方法

要让高亮边与原边完全重叠,需给高亮边添加三个关键属性:

  • constraint=false:禁止这条边影响节点布局
  • samehead/sametail:指定高亮边与原边使用相同的节点端口,强制两条边重叠

修改后的高亮边代码块如下:

{
    edge [color="#95a616" penwidth=5 constraint=false samehead=D sametail=E];
    E -- D;
}

完整修改后代码

graph K5 {
    graph [splines = false, layout=circo];
    edge [penwidth=3];
    node [shape=circle];
    A;
    B;
    C;
    D;
    E;
    {
        edge [color="red"];
        A -- B;
        A -- C;
        C -- D;
        B -- D;
        C -- E;
    }

    {
        edge [color="blue"];
        node [comment="Wildcard node added automatic in EG."];
        D -- E;
        A -- E;
        B -- C;
        B -- E;
        A -- D;
    }

    {
        edge [color="#95a616" penwidth=5 constraint=false samehead=D sametail=E];
        node [comment="Wildcard node added automatic in EG."];
        E -- D;
    }
}

修改后,黄色高亮边会与原蓝色D-E边完全重叠,实现预期的高亮效果。

内容的提问来源于stack exchange,提问作者OwlBandicoot

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 06:46:04