如何使用 tikz 包构建图形?

如何使用 tikz 包构建图形?

我需要使用 tikz 包制作一个类似于以下示例的图形(此示例中的对象形状不是必需的 - 也可以只是矩形):

在此处输入图片描述

答案1

以下是您的解决方案的更简洁的翻译。首先在 中创建节点matrix of nodes。然后使用几个循环将它们连接起来。由于 会matrix of nodes自动命名每个节点,因此无需明确指定名称。通过命名矩阵(M),节点将根据架构命名(M-<row>-<column>)。我尝试用graphs语法将它们连接起来,但图形语法和矩阵似乎并不是天作之合 :(。

这还演示了定制矩阵中的所有节点、一行中的所有节点或单个节点的几种不同方法,并说明了问题中的目标图像所建议的一些其他可能的形状。

\documentclass[border=10pt,multi,tikz,dvipsnames]{standalone}
\usetikzlibrary{positioning,matrix,arrows.meta,shapes.misc,shapes.symbols,shapes.geometric}
\begin{document}
\begin{tikzpicture}[>=Stealth, shape aspect=.25]
  \matrix (M) [matrix of nodes, every node/.style={font=\itshape, anchor=center, text centered, draw, text width=6.1em, minimum height=3em}, column sep=10mm, row sep=10mm, row 5/.append style={rounded rectangle}, row 4/.append style={rounded corners}, row 7/.append style={rounded corners}, row 3/.append style={shape border rotate=90, cylinder}]
  {
    && |[cylinder, shape border rotate=90]| {Imbalanced\\Data} && \\
    && |[rounded corners]| {Data\\Balancing} && \\
    {Balanced\\Data\textsubscript{1}} & {Balanced\\Data\textsubscript{2}} & |[draw=none]|$\cdots$ & {Balanced\\Data\textsubscript{K-1}} & {Balanced\\Data\textsubscript{K}} \\
    Modeling & Modeling & |[draw=none]|$\cdots$  &  Modeling & Modeling \\
    {Classifier\textsubscript{1}} & {Classifier\textsubscript{2}} & |[draw=none]|$\cdots$ & {Classifier\textsubscript{K-1}} & {Classifier\textsubscript{K}} \\
    & & |[rounded rectangle]| {Ensemble\\Classifying} & & \\
    & New Data & Classifying & & \\
    & & |[tape, tape bend top=none]| {Classification\\Results} & & \\
  };
  \coordinate [below=5mm of M-2-3] (p);
  \coordinate [above=5mm of M-6-3] (q);
  \foreach \i/\j in {M-1-3/M-2-3,M-6-3/M-7-3,M-7-3/M-8-3,M-7-2/M-7-3} \draw [->, thick] (\i) -- (\j);
  \foreach \i/\j in {3/4,4/5} \foreach \k in {1,2,4,5} \draw [->, thick] (M-\i-\k) -- (M-\j-\k);
  \draw [thick] (M-2-3) -- (p) \foreach \i in {1,2,4,5} {(M-5-\i) |- (q) (p) -- (p -| M-3-\i)};
  \draw [->, thick] \foreach \i in {1,2,4,5} {(p -| M-3-\i) edge  (M-3-\i)} (q) -- (M-6-3);
\end{tikzpicture}
\end{document}

简洁的代码和奇特的形状

答案2

如果将来有人需要类似的东西,这就是我所构建的。

\tikzstyle{box}=[rectangle, draw=black, rounded corners, text centered, anchor=north, text width=6.1em, minimum height=3em]
\tikzstyle{myarrow}=[->, thick, shorten >=1pt]
\tikzstyle{line}=[-, thick]

\begin{figure}
    \caption{Split schema}
    \label{fig:split}
    \begin{tikzpicture}[node distance=1cm]
        \node (ImbalancedData) [box] {$Imbalanced$\\$Data$};
        \node (DataBalancing) [box, below=0.5cm of ImbalancedData] {$Data$\\$Balancing$};
        \node (AuxNode01) [below=1.4cm of DataBalancing] {$\cdots$};
            \node (BalancedData2) [box, left=of AuxNode01] {$Balanced$\\$Data_{2}$};
            \node (BalancedData1) [box, left=of BalancedData2] {$Balanced$\\$Data_{1}$};
            \node (BalancedDataK-1) [box, right=of AuxNode01] {$Balanced$\\$Data_{K-1}$};
            \node (BalancedDataK) [box, right=of BalancedDataK-1] {$Balanced$\\$Data_{K}$};
        \node (AuxNode02) [below=1.4cm of AuxNode01] {$\cdots$};
            \node (Modeling2) [box, left=of AuxNode02] {$Modeling$};
            \node (Modeling1) [box, left=of Modeling2] {$Modeling$};
            \node (Modeling3) [box, right=of AuxNode02] {$Modeling$};
            \node (Modeling4) [box, right=of Modeling3] {$Modeling$};
        \node (AuxNode03) [below=1.4cm of AuxNode02] {$\cdots$};
            \node (Classifier2) [box, left=of AuxNode03] {$Classifier_{2}$};
            \node (Classifier1) [box, left=of Classifier2] {$Classifier_{1}$};
            \node (ClassifierK-1) [box, right=of AuxNode03] {$Classifier_{K-1}$};
            \node (ClassifierK) [box, right=of ClassifierK-1] {$Classifier_{K}$};
        \node (EnsembleClassifying) [box, below=1.4cm of AuxNode03] {$Ensemble$\\$Classifying$};
        \node (Classifying) [box, below=0.5 of EnsembleClassifying] {$Classifying$};
        \node (NewData) [box, left=1.5 of Classifying] {$New Data$};
        \node (ClassificationResults) [box, below=0.5 of Classifying] {$Classification$\\$Results$};

        \draw[myarrow] (ImbalancedData.south) -- (DataBalancing.north);
        \draw[myarrow] (DataBalancing.south) -- ++(0,-0.5) -| (BalancedData1.north);
        \draw[myarrow] (DataBalancing.south) -- ++(0,-0.5) -| (BalancedData2.north);
        \draw[myarrow] (DataBalancing.south) -- ++(0,-0.5) -| (BalancedDataK-1.north);
        \draw[myarrow] (DataBalancing.south) -- ++(0,-0.5) -| (BalancedDataK.north);
        \draw[myarrow] (BalancedData1.south) -- (Modeling1.north);
        \draw[myarrow] (BalancedData2.south) -- (Modeling2.north);
        \draw[myarrow] (BalancedDataK-1.south) -- (Modeling3.north);
        \draw[myarrow] (BalancedDataK.south) -- (Modeling4.north);
        \draw[myarrow] (Modeling1.south) -- (Classifier1.north);
        \draw[myarrow] (Modeling2.south) -- (Classifier2.north);
        \draw[myarrow] (Modeling3.south) -- (ClassifierK-1.north);
        \draw[myarrow] (Modeling4.south) -- (ClassifierK.north);
        \draw[myarrow] (Classifier1.south) -- ++(0,-0.5) -| (EnsembleClassifying.north);
        \draw[myarrow] (Classifier2.south) -- ++(0,-0.5) -| (EnsembleClassifying.north);
        \draw[myarrow] (ClassifierK-1.south) -- ++(0,-0.5) -| (EnsembleClassifying.north);
        \draw[myarrow] (ClassifierK.south) -- ++(0,-0.5) -| (EnsembleClassifying.north);
        \draw[myarrow] (EnsembleClassifying.south) -- (Classifying.north);
        \draw[myarrow] (NewData.east) -- (Classifying.west);
        \draw[myarrow] (Classifying.south) -- (ClassificationResults.north);
    \end{tikzpicture}
\end{figure}

在此处输入图片描述

相关内容