用 Tikz 绘制图表?

用 Tikz 绘制图表?

我正在尝试使用 Tikz 绘制下图。我在 tex.StackExchange 和 texample.net 中查找了许多答案和示例。我花了很多时间才让它与原始图形相似,我正在寻找一种更高效、更专业的方法来绘制此图形,任何帮助我都会感激不尽。

这是原图: 原始图

这是我的代码:

\documentclass[border=10pt]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows, arrows.meta, positioning,shadows, shapes}

\definecolor{myblue}{HTML}{bcc9cf}
\definecolor{mygrey}{HTML}{d8dfe2}
\definecolor{mywhite}{HTML}{ffffff}

\tikzset{
    RoundedRect/.style={draw, very thick, minimum width=8em, minimum height=8ex, inner ysep=2mm, outer sep=1pt, execute at end node=\vphantom{g},font=\Huge,drop shadow, rounded corners},
    SignalToEast/.style={RoundedRect, inner xsep=-2pt,signal, signal to= east},
    SignalToWest/.style={RoundedRect, inner xsep=-2pt,signal, signal to= west},     
    TextBox/.style={font=\scriptsize\bfseries,  align=center,text width= 2cm},
    smallRect/.style={very thick,draw,rectangle,blue},
    smallElli/.style={very thick,draw,ellipse,red}
}   

\begin{document}
    \begin{tikzpicture}[node distance = 10mm and 20mm]
    \node (1) [SignalToEast,fill=myblue] {1};
    \node (2) [RoundedRect,fill=mygrey,above right=of 1] {2};
    \node (3) [RoundedRect,fill=mygrey,below right=of 1] {3};
    \node (4) [SignalToWest,fill=myblue,below right=of 2] {4};
    \node (5) [left=2cm of 1] {};
    \node (6) [right=3.5cm of 4] {};            
    \node (7) [TextBox,below =1.5cm of 1.south west] {Description of this node};
    \node (8) [TextBox,below=.5cm of 4.south east] {Description of this node};
        
    % Some auxiliary coordinates for nodNam13 arrows 
    \node (coordNam1) [below=.1mm of 5.south] {};
    
    \node (aux1) [smallRect,right=15mm of coordNam1,inner sep=1.5pt] {7};
    \node (aux2) [smallRect,right=11mm of coordNam1,inner sep=1.5pt] {8};
    \node (aux3) [smallElli,right=6mm of coordNam1,inner sep=1pt] {7};
    \node (aux3) [right=1mm of coordNam1,inner sep=1pt] {$\ldots$};
    %--------------------------------------------------------------------------------
    \node (coord1) [right=of 4.10] {};
    
    \node (COORD1) [right=.5cm of coord1,inner sep=1pt,blue,yshift=1mm] {$\mathcal{F(\omega}^x_1)$};
    \node (COORD2) [right=-.5cm of coord1,inner sep=1pt,blue,yshift=1mm] {$\mathcal{F(\omega}^y_1)$};
    \node (COORD3) [right=-1.5cm of coord1,inner sep=1pt,red,yshift=1mm] {$\mathcal{F(\omega}^x_2)$};
    \node (COORD4) [right=-2cm of coord1,inner sep=1pt,yshift=.5mm] {$\ldots$};
    %--------------------------------------------------------------------------------
    \node (3coord1) [left=0mm of 3.north west,inner sep=0pt] {};
        
    \node (3COORD1) [smallElli,left=4mm of 3coord1,inner sep=1pt] {4};
    \node (3COORD2) [smallElli,left=8mm of 3coord1,inner sep=1pt] {5};
    \node (3COORD3) [smallElli,left=12mm of 3coord1,inner sep=1pt] {6};
    \node (3COORD4) [left=16mm of 3coord1,inner sep=1pt] {$\mathcal{\omega}^y_4$};
    
    \node (3COORD1) [smallRect,left=4mm of 3coord1,inner sep=1.5pt,yshift=-6mm] {4};
    \node (3COORD2) [smallRect,left=8mm of 3coord1,inner sep=1.5pt,yshift=-6mm] {5};
    \node (3COORD3) [smallRect,left=12mm of 3coord1,inner sep=1.5pt,yshift=-6mm] {6};
    \node (3COORD4) [left=16mm of 3coord1,inner sep=1pt,yshift=-6mm] {$\mathcal{\omega}^y_4$};
    
    \node (3COORD1) [smallElli,left=1mm of 3coord1,inner sep=1pt,yshift=-13mm] {2};
    \node (3COORD2) [smallElli,left=5mm of 3coord1,inner sep=1pt,yshift=-13mm] {3};
    \node (3COORD3) [smallElli,left=9mm of 3coord1,inner sep=1pt,yshift=-13mm] {4};
    \node (3COORD4) [left=13mm of 3coord1,inner sep=1pt,yshift=-13mm] {$\mathcal{\omega}^y_2$};
    
    \node (3COORD1) [smallRect,left=1mm of 3coord1,inner sep=1.5pt,yshift=-18mm] {2};
    \node (3COORD2) [smallRect,left=5mm of 3coord1,inner sep=1.5pt,yshift=-18mm] {3};
    \node (3COORD3) [smallRect,left=9mm of 3coord1,inner sep=1.5pt,yshift=-18mm] {4};
    \node (3COORD4) [left=13mm of 3coord1,inner sep=1pt,yshift=-18mm] {$\mathcal{\omega}^x_2$};
    
    %******************************************************************************
    \node (below4) [below= 1mm of 4] { };
    \node (FW1) [left=2mm of below4,inner sep=.5pt] {$\mathcal{F(\omega}^x_2)$};
    
    \node (FW2) [left=6mm of below4,inner sep=.5pt,yshift=-5mm] {$\mathcal{F(\omega}^y_2)$};
    
    \node (FW3) [left=11mm of below4,inner sep=.5pt,yshift=-9mm] {$\mathcal{F(\omega}^x_4)$};
    
    \node (FW4) [left=16mm of below4,inner sep=.5pt,yshift=-14mm] {$\mathcal{F(\omega}^y_4)$};
    %******************************************************************************
    
    \draw[circle,below=2mm of coordNam1] (5.east |- 1.west);
    \draw[-latex,very thick] (1) -- (2);
    \draw[-Stealth,very thick] (1) -- (3);
    \draw[-latex,very thick] (3) -- (4);
    \draw[-latex,very thick] (2) -- (4);
    \draw[-latex,very thick] (5) -- (1);
    \draw[-latex,very thick] (4) -- (6);
    \draw[dash dot,very thick] (8) -- (4.text);
    %-------------------------------------------------------------------------------
    \node (darect) [draw=blue, very thick, minimum width=5.5em, minimum height=8ex, inner ysep=2mm, outer sep=1pt,below =3mm of 1.center,dashed,rounded corners] {};
    
    \node (x1) [smallRect,below=3.5mm of 1.south east,xshift=-5mm,fill=mywhite,inner sep=1.5pt] {4};
    \node (x2) [smallRect,below=3.5mm of 1.south,fill=mywhite,inner sep=1.5pt] {5};
    \node (x3) [smallRect,below=3.5mm of 1.south west,xshift=5mm,fill=mywhite,inner sep=1.5pt] {6};
    
    \node (xx1) [smallElli,below=-2mm of 1.south east,xshift=-5mm,fill=mywhite,inner sep=1pt] {4};
    \node (xx2) [smallElli,below=-2mm of 1.south,fill=mywhite,inner sep=1pt] {5};
    \node (xx3) [smallElli,below=-2mm of 1.south west,xshift=5mm,fill=mywhite,inner sep=1pt] {6};
    
    \draw[dash dot,very thick] (7) -- (darect);
    \end{tikzpicture}

\end{document}

在此处输入图片描述

我正在寻找一种无需多次尝试即可定位和连接节点的技术。在我的代码中,我用 mm 更改节点的位置,直到将其放置在正确的位置。有没有一种方法可以做到这一点而不需要尝试不同的值?

答案1

你已经做出了非常好的初步努力,我只是对你的 MWE 做了一些小的改进。

  1. 我注意到您使用了太多节点。我通过定义新命令\smallElli和 来对许多节点进行分组\smallRect
  2. 我已替换\mathcal{\omega}\omega(它们给出了相同的结果)。
  3. 我已经引入了一个\myF命令\mathcal{F}
  4. 我已使用沿着绘制操作的节点将文本沿着箭头从节点 3 放置到节点 4。

希望这可以帮助!

\documentclass[border=10pt]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows, arrows.meta, positioning,shadows, shapes}

\definecolor{myblue}{HTML}{bcc9cf}
\definecolor{mygrey}{HTML}{d8dfe2}
\definecolor{mywhite}{HTML}{ffffff}

\tikzset{
    RoundedRect/.style={draw, very thick, minimum width=8em, minimum height=8ex, inner ysep=2mm, outer sep=1pt, execute at end node=\vphantom{g},font=\Huge,drop shadow, rounded corners},
    SignalToEast/.style={RoundedRect, inner xsep=-2pt,signal, signal to= east},
    SignalToWest/.style={RoundedRect, inner xsep=-2pt,signal, signal to= west},     
    TextBox/.style={font=\scriptsize\bfseries,  align=center,text width= 2cm},
    smallRect/.style={very thick,draw,rectangle,blue},
    smallElli/.style={very thick,draw,ellipse,red}
}

\newcommand{\smallElli}[1]{\tikz{\node[ellipse,draw=red,fill=white,solid,very thick,inner sep=1pt]{{\color{red}#1}};}}
\newcommand{\smallRect}[1]{\tikz{\node[rectangle,draw=blue,fill=white,solid,very thick,inner sep=2pt,rounded corners=0pt]{{\color{blue}#1}};}}
\newcommand{\myF}{\ensuremath{\mathcal{F}}}

\begin{document}
    \begin{tikzpicture}[node distance = 10mm and 20mm]
        \node (1) [SignalToEast,fill=myblue] {1};
        \node (2) [RoundedRect,fill=mygrey,above right=of 1] {2};
        \node (3) [RoundedRect,fill=mygrey,below right=of 1] {3};
        \node (4) [SignalToWest,fill=myblue,below right=of 2] {4};
        \node (5) [left=2cm of 1] {};
        \node (6) [right=3.5cm of 4] {};            
        \node (7) [TextBox,below =1.5cm of 1.south west] {Description of this node};
        \node (8) [TextBox,below=.5cm of 4.south east] {Description of this node};
        
        % Some auxiliary coordinates for nodNam13 arrows 
        \node (coordNam1) [below=.1mm of 5.south] {};
        
%        \node (aux1) [smallRect,right=15mm of coordNam1,inner sep=1.5pt] {7};
%        \node (aux2) [smallRect,right=11mm of coordNam1,inner sep=1.5pt] {8};
%        \node (aux3) [smallElli,right=6mm of coordNam1,inner sep=1pt] {7};
%        \node (aux3) [right=1mm of coordNam1,inner sep=1pt] {$\ldots$};
        \node [below=1pt of 5,anchor=north west,inner sep=0pt, baseline=0pt] {$\ldots$ \smallElli{7} \smallRect{8} \smallRect{7}};
        %--------------------------------------------------------------------------------
%        \node (coord1) [right=of 4.10] {};
%        
%        \node (COORD1) [right=.5cm of coord1,inner sep=1pt,blue,yshift=1mm] {$\mathcal{F(\omega}^x_1)$};
%        \node (COORD2) [right=-.5cm of coord1,inner sep=1pt,blue,yshift=1mm] {$\mathcal{F(\omega}^y_1)$};
%        \node (COORD3) [right=-1.5cm of coord1,inner sep=1pt,red,yshift=1mm] {$\mathcal{F(\omega}^x_2)$};
%        \node (COORD4) [right=-2cm of coord1,inner sep=1pt,yshift=.5mm] {$\ldots$};
        \node [above right = 1pt and 1pt of 4.east] {$\ldots {\color{red} \myF(\omega_2^x)} {\color{blue} \myF(\omega_1^y) \myF(\omega_1^x)}$};
        %--------------------------------------------------------------------------------
%        \node (3coord1) [left=0mm of 3.north west,inner sep=0pt, anchor=south west] {};
        
%        \node (3COORD1) [smallElli,left=4mm of 3coord1,inner sep=1pt] {4};
%        \node (3COORD2) [smallElli,left=8mm of 3coord1,inner sep=1pt] {5};
%        \node (3COORD3) [smallElli,left=12mm of 3coord1,inner sep=1pt] {6};
%        \node (3COORD4) [left=16mm of 3coord1,inner sep=1pt] {$\mathcal{\omega}^y_4$};
        
%        \node (3COORD1) [smallRect,left=4mm of 3coord1,inner sep=1.5pt,yshift=-6mm] {4};
%        \node (3COORD2) [smallRect,left=8mm of 3coord1,inner sep=1.5pt,yshift=-6mm] {5};
%        \node (3COORD3) [smallRect,left=12mm of 3coord1,inner sep=1.5pt,yshift=-6mm] {6};
%        \node (3COORD4) [left=16mm of 3coord1,inner sep=1pt,yshift=-6mm] {$\mathcal{\omega}^y_4$};
        \node [below left = 1.25em and 3em of 3.west,anchor=south east,align=center,inner sep=0pt] {$\omega_4^y$ \smallElli{6} \smallElli{5} \smallElli{4}\\$\omega_4^x$ \smallRect{6} \smallRect{5} \smallRect{4}};
        
%        \node (3COORD1) [smallElli,left=1mm of 3coord1,inner sep=1pt,yshift=-13mm] {2};
%        \node (3COORD2) [smallElli,left=5mm of 3coord1,inner sep=1pt,yshift=-13mm] {3};
%        \node (3COORD3) [smallElli,left=9mm of 3coord1,inner sep=1pt,yshift=-13mm] {4};
%        \node (3COORD4) [left=13mm of 3coord1,inner sep=1pt,yshift=-13mm] {$\mathcal{\omega}^y_2$};
        
%        \node (3COORD1) [smallRect,left=1mm of 3coord1,inner sep=1.5pt,yshift=-18mm] {2};
%        \node (3COORD2) [smallRect,left=5mm of 3coord1,inner sep=1.5pt,yshift=-18mm] {3};
%        \node (3COORD3) [smallRect,left=9mm of 3coord1,inner sep=1.5pt,yshift=-18mm] {4};
%        \node (3COORD4) [left=13mm of 3coord1,inner sep=1pt,yshift=-18mm] {$\mathcal{\omega}^x_2$};

        \node [below left = 1em and 0.25em of 3.west,anchor=north east,align=center] {$\omega_2^y$ \smallElli{4} \smallElli{3} \smallElli{2}\\$\omega_2^x$ \smallRect{4} \smallRect{3} \smallRect{2}};
        
        %******************************************************************************
%        \node (below4) [below= 1mm of 4] { };
%        \node (FW1) [left=2mm of below4,inner sep=.5pt] {$\mathcal{F(\omega}^x_2)$};
%        
%        \node (FW2) [left=6mm of below4,inner sep=.5pt,yshift=-5mm] {$\mathcal{F(\omega}^y_2)$};
%        
%        \node (FW3) [left=11mm of below4,inner sep=.5pt,yshift=-9mm] {$\mathcal{F(\omega}^x_4)$};
%        
%        \node (FW4) [left=16mm of below4,inner sep=.5pt,yshift=-14mm] {$\mathcal{F(\omega}^y_4)$};
        %******************************************************************************
        
        \tikzstyle{temp} = [below=1em, inner sep=0pt, anchor=north west]
        \draw[circle,below=2mm of coordNam1] (5.east |- 1.west);
        \draw[-latex,very thick] (1) -- (2);
        \draw[-Stealth,very thick] (1) -- (3);
        \draw[-latex,very thick] (3) -- node [pos=0.1, temp] {$\myF(\omega_4^y)$} node [pos=0.4, temp] {$\myF(\omega_4^x)$} node [pos=0.7, temp] {$\myF(\omega_2^y)$} node [pos=1, temp] {$\myF(\omega_2^x)$} (4);
        \draw[-latex,very thick] (2) -- (4);
        \draw[-latex,very thick] (5) -- (1);
        \draw[-latex,very thick] (4) -- (6);
        \draw[dash dot,very thick] (8) -- (4.text);
        %-------------------------------------------------------------------------------
        \node (darect) [rectangle, draw=blue, dashed, rounded corners, very thick, below = 3mm of 1.center, align=center] {\smallElli{6} \smallElli{5} \smallElli{4}\\ \smallRect{6} \smallRect{5} \smallRect{4}};
        
%        \node (x1) [smallRect,below=3.5mm of 1.south east,xshift=-5mm,fill=mywhite,inner sep=1.5pt] {4};
%        \node (x2) [smallRect,below=3.5mm of 1.south,fill=mywhite,inner sep=1.5pt] {5};
%        \node (x3) [smallRect,below=3.5mm of 1.south west,xshift=5mm,fill=mywhite,inner sep=1.5pt] {6};
%        
%        \node (xx1) [smallElli,below=-2mm of 1.south east,xshift=-5mm,fill=mywhite,inner sep=1pt] {4};
%        \node (xx2) [smallElli,below=-2mm of 1.south,fill=mywhite,inner sep=1pt] {5};
%        \node (xx3) [smallElli,below=-2mm of 1.south west,xshift=5mm,fill=mywhite,inner sep=1pt] {6};
        
        \draw[dash dot,very thick] (7) -- (darect);
    \end{tikzpicture}
    
\end{document}

输出: 在此处输入图片描述

编辑

按照@SebGlav 的建议使用chains库可能会更加高效。我对此并不熟练。

相关内容