Tikzpicture 水平树 - 如何重组女儿

Tikzpicture 水平树 - 如何重组女儿

我有一个水平树,代表一个多期财务模型。我希望每个父级都收敛到与其上方父级下行相同的子级,反之亦然。我附上了一份代码副本和一张代码图片,希望您清楚我想要做什么。

例如,在附图中,我希望两个 9.54 节点是来自前两个父节点的一个节点...

在此处输入图片描述

\begin{tikzpicture}[grow=right, edge from parent/.style={draw,-latex}]
\node[square][label={[label distance=0.2cm]90:t=0}] {11.45}
    child {
    node[square] {19.08} 
        child {
        node[square] {29.54}     
            child {
            node[square] {40}        
                    }
            child {
            node[square] {20}         
                    }   
                }
        child {
        node[square] {9.54} 
            child {
            node[square] {20}        
                    }
            child {
            node[square] {20}         
                    }        
                }
        }
    child {
    node[square][label={[label distance=0.2cm]90:t=1}] {4.53}        
        child {
        node[square] {9.54} 
            child {
            node[square] {0}       
                    }
            child {
            node[square] {0}         
                    }       
                }
        child {
        node[square][label={[label distance=0.2cm]90:t=2}] {0}
            child {
            node[square] {0}        
                    }
            child {
            node[square][label={[label distance=0.2cm]90:t=3}] {0}         
                    }         
                }
        };
\end{tikzpicture}

答案1

编辑:好吧,我需要三次迭代我的答案才能弄清楚,你的问题是什么……现在我只需要重新编辑第三个解决方案并删除它的步骤。所需形式通过以下方式获得:

  • 选择适当的同级和级别距离
  • 在重叠节点中,我只在其中靠近图片代码开头的节点中写入内容,第二个节点我留空。这消除了您在问题和自己的答案中使用透明度的问题。

MWE 包含指向跳过内容的节点的指针,其内容如下:

\documentclass[border=3mm,
               tikz,
               ]{standalone}
    \begin{document}
\begin{tikzpicture}[
                   grow = right,
edge from parent/.style = {draw,-latex},
         label distance = 2mm,
      every node/.style = {minimum width=2em, inner sep=2pt},
         level distance = 31mm,
       sibling distance = 13mm,
                     ]
\node[label=90:{$t=0$}] {11.45}
    child {node {19.08}
        child {node {29.54}
            child {node {40}}
            child {node {20}}
                }
        child {node {}}%<---------------- already printed
            }
    child {node[label=90:{$t=1$}] {4.53}
        child {node {9.54}
            child {node {}}%<------------ already printed
            child {node {0}}
                }
        child {node[label=90:{$t=2$}] {0}
            child {node {}}%<------------ already printed
            child {node[label=90:{$t=3$}] {0}}
                }
                };
\end{tikzpicture}
    \end{document}

在此处输入图片描述

答案2

非常感谢,我有点傻,没有意识到我可以将兄弟距离设置为常数。对两个重叠节点使用 0.75 的不透明度,对三个重叠节点使用 0.5 的不透明度,效果不错。

合并女儿

\documentclass[border=3mm,tikz]{standalone}

\begin{document}
\begin{tikzpicture}[grow=right, edge from parent/.style={draw,-latex}, label distance = 0.2cm,
  level 1/.style = {level distance=3.5cm, sibling distance=22mm},
  level 2/.style = {level distance=3.5cm, sibling distance=22mm},
  level 3/.style = {level distance=3.5cm, sibling distance=22mm},
  ]
  \node[label=90:{t=0}] {11.45}
  child {node {19.08}
    child {node {29.54}
      child {node {40}}
      child {node[opacity=.5] {20}}
    }
    child {node[opacity=.75] {9.54}
      child {node[opacity=.5] {20}}
      child {node[opacity=.5] {0}}
    }
  }
  child {node[label=90:{t=1}] {4.53}
    child {node[opacity=.75] {9.54}
      child {node[opacity=.5] {20}}
      child {node[opacity=.5] {0}}
    }
    child {node[label=90:{t=2}] {0}
      child {node[opacity=.5] {0}}
      child {node[label=90:{t=3}] {0}}
    }
  };
\end{tikzpicture}
\end{document}

相关内容