在 tikz 中分离“节点下方”

在 tikz 中分离“节点下方”

我有以下代码

 \usepackage{tikz}
 \tikzstyle{comp1} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=3cm, 
  fill=gray!50] 
  \tikzstyle{comp2} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=3cm] 
  \tikzstyle{comp3} = [draw, ellipse, minimum height=1cm, minimum width=3cm, fill=gray!10, text centered]

  \tikzstyle{arrow} = [thick,->,>=stealth]

\begin{figure}
\centering

\begin{tikzpicture}[node distance=2.5cm]

%%% NODES %%%
\node (dataset)     [comp1]         {Dataset};
\coordinate[below of=dataset] (c);
\node (train)         [comp2, left of=c]      {Train};
\node (test)        [comp2, right of=c]     {Test};
\coordinate[below of=train] (d);
\node (train_train)       [comp3, left of=d]     {Train};
\node (train_validation)  [comp3, right of=d] {Validation};
\coordinate[below of=test] (e);
\node (test_train)       [comp3, left of = e]  {Train};
\node (test_validation)  [comp3, right of = e ] {Validation};
%%% ARROWS %%%
\draw [arrow] (dataset) -- (train);
\draw [arrow] (dataset) -- (test);
\draw [arrow] (train) -- (train_train);
\draw [arrow] (train) -- (train_validation);
\draw [arrow] (test) -- (test_train);
\draw [arrow] (test) -- (test_validation);
\end{tikzpicture}
\end{figure}

结果是:

图表

当我需要它时:

>              Dataset
>      train             test 
>  train validation  train validation

我知道第 3 级的测试和验证是重叠的,因为我使用的是第 2 级的相对坐标,但我不知道如何修复它。任何帮助都非常感谢!

答案1

欢迎来到 TeX.SX!请使您的代码可编译(如果可能),或者至少使用\documentclass{...}所需的\usepackage\begin{document}和来完成它\end{document}。这对您来说可能看起来很乏味,但请想想它为愿意帮助您的 TeX.SX 用户带来的额外工作。帮助他们帮助您:消除您和问题解决方案之间的障碍。

为了解决您的问题,我只需指定训练节点和测试节点的节点距离。

\documentclass{standalone}
\usepackage{tikz}

\usetikzlibrary{positioning,shapes}

\tikzstyle{comp1} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=3cm, fill=gray!50] 
\tikzstyle{comp2} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=3cm] 
\tikzstyle{comp3} = [draw, ellipse, minimum height=1cm, minimum width=3cm, fill=gray!10, text centered]
\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{document}
  \begin{tikzpicture}[node distance=2.5cm]
  %%% NODES %%%
  \node (dataset)     [comp1]         {Dataset};
  \coordinate[below of=dataset] (c);
  \node (train)         [comp2, left =3.75cm of c]      {Train};
  \node (test)        [comp2, right =3.75cm of c]     {Test};
  \coordinate[below of=train] (d);
  \node (train_train)       [comp3, left of=d]     {Train};
  \node (train_validation)  [comp3, right of=d] {Validation};
  \coordinate[below of=test] (e);
  \node (test_train)       [comp3, left of = e]  {Train};
  \node (test_validation)  [comp3, right of = e ] {Validation};
  %%% ARROWS %%%
  \draw [arrow] (dataset) -- (train);
  \draw [arrow] (dataset) -- (test);
  \draw [arrow] (train) -- (train_train);
  \draw [arrow] (train) -- (train_validation);
  \draw [arrow] (test) -- (test_train);
  \draw [arrow] (test) -- (test_validation);
  \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这是一个使用 的解决方案。您可以根据需要forest调整水平间距 ( s sep) 或垂直间距 ( )。l sep

在此处输入图片描述

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{forest}
for tree={draw, thick, edge={thick, -stealth}, s sep=1cm, l sep=1cm, minimum height=1cm, minimum width=3cm,
    if n children=0 
        {ellipse, fill=gray!10} % if it's a leaf
        {rounded corners}} % otherwise...
[Dataset, fill=gray!50
    [Train
        [Train][Validation]
    ]
    [Test
        [Train][Validation]
    ]
]
\end{forest}

\end{document}

答案3

这是为您提供的第二种方法,使用 tikz 库shapes.geometricxshift。部分grid用于定位。

在此处输入图片描述

\documentclass[10pt, border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}% <<< contains ellipses
 
 
\begin{document}
    \tikzstyle{comp1} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=3cm, fill=gray!50] 
    \tikzstyle{comp2} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=3cm] 
    \tikzstyle{comp3} = [draw, ellipse, minimum height=1cm, minimum width=3cm, fill=gray!10, text centered]

  \tikzstyle{arrow} = [thick,->,>=stealth]



\begin{tikzpicture}[node distance=2.5cm]
\draw [help lines] (0,0) grid (5, -5);% just to indicate coordinates

    %%% NODES %%%
    \node (dataset) [comp1]         {Dataset};
    \coordinate[below of=dataset] (c);
    \node (train)   [comp2, left of=c,  xshift=-2cm]    {Train};% using xshift
    \node (test)    [comp2, right of=c, xshift= 2cm]    {Test};%  same
    
    \coordinate[below of=train] (d);
    \node (train_train)       [comp3, left of=d]     {Train};
    \node (train_validation)  [comp3, right of=d] {Validation};
    \coordinate[below of=test] (e);
    \node (test_train)       [comp3, left of = e]  {Train};
    \node (test_validation)  [comp3, right of = e ] {Validation};
    %%% ARROWS %%%
    \draw [arrow] (dataset) -- (train);
    \draw [arrow] (dataset) -- (test);
    \draw [arrow] (train) -- (train_train);
    \draw [arrow] (train) -- (train_validation);
    \draw [arrow] (test) -- (test_train);
    \draw [arrow] (test) -- (test_validation);
\end{tikzpicture}


\end{document}

相关内容