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

求助:如何将Python中Dot脚本生成的Fishbone Diagram优化为标准样式?

调整Dot脚本生成标准鱼骨图(Ishikawa图)

我正在Python中创建鱼骨图(又称因果图、Ishikawa图),目前用论坛帖子里的Dot脚本生成了一个版本,想要调整为标准鱼骨图样式。因为对Dot语言不熟悉,恳请帮忙修改以下脚本:

graph W {
      graph [splines=line]
      node [shape=underline]
      {
        rank=same
        node [shape=rect style=invis centerline=true]
        edge [headclip=false tailclip=false]
        p1 [group=g1]
        p2 [group=g2]
        p3 [group=g3]
        pstart:c -- p1:c --  p2:c --  p3:c
        pend [label=" Defect XXX" shape=plain style=solid]
        p3:c -- pend:w [dir=forward arrowhead=normal]
      }
      {
        rank=min
        node [shape=plain]    
        measurements  materials personnel
      }
      {
      node [group=g1]
      measurements:se -- calibration:e -- microscopes:e -- inspectors:e
      inspectors:e -- p1:c  [headclip=false]
      }
      {
      node [group=g2]
      materials:s--alloys:e--Lubricants:e--Suppliers:e 
      Suppliers:e -- p2:c [headclip=false]
      }
      {
      node [group=g3]
      personnel:s--shifts:e--training:e--operators:e--"another thing":e -- p3:c [headclip=false]
      }
      {
        rank=max
        node [shape=plaintext]
        Environment Methods Machines
      }
      {
      node [group=g1]
      p1 -- Humidity:e [tailclip=false]
      Humidity:e -- temperature:e -- Environment:n
      }
      {
      node [group=g2]
      p2 -- Angle:e  [tailclip=false]
      Angle:e --Engager:e-- Brake:e-- Methods:n 
      }
      {
      node [group=g3]
      p3 -- "blade wear":e  [tailclip=false]
      "blade wear":e -- speed:e -- Machines:n
      }
    }
   
 BEGIN{int nIndx=0;
  float centerY=-999.9, dY, angleAdj, minY=9999, maxY=-9999;
  node_t Node[];
  graph_t theRoot, subG;
  void doErrs(string eString){
     printf(2, "Error:: %s\n", eString);
     print("// Error:: %s\n", eString); // temporary, for debugging
  }
}
N{
   Node[++nIndx]=$;
   if ((hasAttr($, "centerline")) && ($.centerline!="")){
      centerY=$.Y;
   }
   if ($.Y >maxY)
     maxY=$.Y;
   if ($.Y < minY)
     minY=$.Y;
}

    END_G{
      if (centerY==-999.9){
          estr="\"centerline\" attribute is missing"; doErrs(estr);;
          exit(9);
      }
      $G.bb="";
      for (Node[nIndx]){
        print("//  pos before: ",Node[nIndx].pos);
        dY=Node[nIndx].Y-centerY;
        print("//  dY (1) : ", dY);
    
        if (dY<0){
          dY=-dY;
        }
        print("//  dY (2) : ", dY);
        Node[nIndx].pos=(string)(Node[nIndx].X - .25*dY) + "," + (string)Node[nIndx].Y;
        print("//  pos after: ", Node[nIndx].pos);
      }
    }

修改后的Dot脚本

下面是调整为标准鱼骨图样式的脚本,核心优化点包括:主轴线从左到右延伸、上下分支垂直对称、简化布局逻辑、统一节点层级样式:

digraph Ishikawa {
    graph [splines=ortho, rankdir=LR, nodesep=0.5, ranksep=0.8]
    node [shape=plaintext, fontsize=10]
    
    # 主轴线与核心问题节点
    core [label="Defect XXX", shape=rect, style=filled, fillcolor=lightgray]
    main [shape=point]
    main -> core [arrowhead=normal]
    
    # 上层分支(人员、方法、机器)
    {rank=same; main; personnel_top; methods_top; machines_top}
    main:n -> personnel_top [dir=none]
    main:n -> methods_top [dir=none]
    main:n -> machines_top [dir=none]
    
    personnel_top [shape=point]
    personnel_top:n -> personnel [label="人员"]
    personnel_top -> shifts_top [dir=none]
    shifts_top -> training_top [dir=none]
    training_top -> operators_top [dir=none]
    operators_top -> "another thing_top" [dir=none]
    {rank=same; shifts_top; training_top; operators_top; "another thing_top"}
    shifts_top [label="轮班"]
    training_top [label="培训"]
    operators_top [label="操作员"]
    "another thing_top" [label="其他事项"]
    
    methods_top [shape=point]
    methods_top:n -> methods [label="方法"]
    methods_top -> angle_top [dir=none]
    angle_top -> engager_top [dir=none]
    engager_top -> brake_top [dir=none]
    {rank=same; angle_top; engager_top; brake_top}
    angle_top [label="角度"]
    engager_top [label="接合器"]
    brake_top [label="刹车"]
    
    machines_top [shape=point]
    machines_top:n -> machines [label="机器"]
    machines_top -> "blade wear_top" [dir=none]
    "blade wear_top" -> speed_top [dir=none]
    {rank=same; "blade wear_top"; speed_top}
    "blade wear_top" [label="刀片磨损"]
    speed_top [label="速度"]
    
    # 下层分支(测量、材料、环境)
    {rank=same; main; measurements_bottom; materials_bottom; environment_bottom}
    main:s -> measurements_bottom [dir=none]
    main:s -> materials_bottom [dir=none]
    main:s -> environment_bottom [dir=none]
    
    measurements_bottom [shape=point]
    measurements_bottom:s -> measurements [label="测量"]
    measurements_bottom -> calibration_bottom [dir=none]
    calibration_bottom -> microscopes_bottom [dir=none]
    microscopes_bottom -> inspectors_bottom [dir=none]
    {rank=same; calibration_bottom; microscopes_bottom; inspectors_bottom}
    calibration_bottom [label="校准"]
    microscopes_bottom [label="显微镜"]
    inspectors_bottom [label="检验员"]
    
    materials_bottom [shape=point]
    materials_bottom:s -> materials [label="材料"]
    materials_bottom -> alloys_bottom [dir=none]
    alloys_bottom -> lubricants_bottom [dir=none]
    lubricants_bottom -> suppliers_bottom [dir=none]
    {rank=same; alloys_bottom; lubricants_bottom; suppliers_bottom}
    alloys_bottom [label="合金"]
    lubricants_bottom [label="润滑剂"]
    suppliers_bottom [label="供应商"]
    
    environment_bottom [shape=point]
    environment_bottom:s -> environment [label="环境"]
    environment_bottom -> humidity_bottom [dir=none]
    humidity_bottom -> temperature_bottom [dir=none]
    {rank=same; humidity_bottom; temperature_bottom}
    humidity_bottom [label="湿度"]
    temperature_bottom [label="温度"]
}

关键调整说明

  1. 布局方向:设置rankdir=LR让主轴线从左到右延伸,符合标准鱼骨图的阅读逻辑
  2. 分支结构:用rank=same保证每个分支的子节点水平对齐,主轴线上下的分支分别从main:n和main:s引出,形成垂直对称的鱼骨臂
  3. 节点样式:核心问题节点用填充样式突出,子节点统一用plaintext,层级关系更清晰
  4. 简化脚本:去掉了原脚本中复杂的位置调整代码,依靠Graphviz自带的布局规则实现对称效果

你可以将这个脚本传入Python的graphviz库中渲染,即可得到标准的鱼骨图样式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 10:55:21