我如何绘制带有节点和箭头的图表?

我如何绘制带有节点和箭头的图表?

我该如何画这个?

在此处输入图片描述

相关图表(更复杂)在这里Tikz绘制神经网络轮廓

我确实尝试修改@Zarco 提供的代码,但我只能修改节点。最后我对箭头感到很困惑。

    \documentclass[tikz, margin=3mm]{standalone}
    \usetikzlibrary{chains, positioning}

    \begin{document}
        \begin{tikzpicture}[shorten >=1pt,->, draw=black!50,
            node distance = 6mm and 15mm,
              start chain = going below,
    every pin edge/.style = {<-,shorten <=1pt},
            neuron/.style = {circle, draw=black, fill=#1,   % <--- changed
                             minimum size=17pt, inner sep=0pt,
                             on chain},
             annot/.style = {text width=4em, align=center}
                            ]
    % Draw the input layer nodes
    \foreach \i in {1,...,3}
        \node[neuron=green!50,
              pin=180:] (I-\i)    {};
    % Draw the hidden layer nodes
        \node[neuron=blue!50,
              above right=6mm and 15mm of I-1.center] (H-1)     {$x_{1}$};
    \foreach \i [count=\j from 1] in {2}
        \node[neuron=blue!50,
              below=of H-\j]      (H-\i)    {$x_{\i}$};
    % Draw the output layer node
        \node[neuron=red!50,
              pin= {[pin edge=->]0:Output \#1},
              right=of I-2 -| H-1]  (O-1)   {$x_{1}$};
        \node[neuron=red!50,                            % <--- changed
              pin= {[pin edge=->]0:Output \#2},
              below=of O-1]         (O-2)   {$x_{2}$};  % <--- changed
\node[neuron=red!50,
              pin= {[pin edge=->]0:Output \#3},
              below=of 0-2]  (O-3)   {$x_{1}$};

    % Connect input nodes with hidden nodes and
    %  hiden nodes with output nodes with the output layer
        \foreach \i in {1,...,4}
            \foreach \j in {1,...,5}
    {
        \path (I-\i) edge (H-\j)
        \ifnum\i<3                  % <--- changed
              (H-\j) edge (O-\i)
        \fi;
    }
        \end{tikzpicture}
    \end{document}  

先感谢您。

答案1

chains库是一个强大的工具,但可能不太适合这样的图表。这是一种让事情变得更加自动化和易于访问的尝试。请阅读代码中的注释。输入和输出存储在列表中,神经元的数量也是如此。

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{quotes,positioning}
\begin{document}
    \begin{tikzpicture}[shorten >=1pt,->, draw=black!50,>=stealth,
        every pin edge/.style = {<-,shorten <=1pt},
        every pin/.style={font=\small\sffamily,text=gray,inner sep=1pt},
        neuron/.style = {circle, draw=black, fill=#1,
                         minimum size=20pt, inner sep=0pt},
        neuron/.default=white,               
        layer 1/.style={neuron=green!50,pin={[alias=el1-\i]180:\myedgelabel}},
        layer 2/.style={neuron=blue!50},
        layer 3/.style={neuron=red!50,pin={[pin edge={->},alias=el3-\i]0:\myedgelabel}},
         annot/.style = {text width=4em, align=center},
         neuron list/.store in=\NeuronList,
         neuron list={3,2,3}, %<- # of neurons at each layer
         input list/.store in=\InputList,
         input list={200,150,200},%<- inputs
         output list/.store in=\OutputList,
         output list={200,250,100},%<- outputs
                        ]
% Draw the input layer nodes
 \pgfmathtruncatemacro{\imax}{{\NeuronList}[0]}
 \path foreach \i in {1,...,\imax}
    {(0,{(\imax/2-1/2-\i)*1.5})
    [/utils/exec=\pgfmathsetmacro{\myedgelabel}{{\InputList}[\i-1]}]
     node[layer 1] (P-\i)    {$P_\i$}};  
% Draw the hidden layer nodes
 \pgfmathtruncatemacro{\imax}{{\NeuronList}[1]}
 \path foreach \i in {1,...,\imax}
    {(25mm,{(\imax/2-1/2-\i)*1.5}) node[layer 2] (T-\i)    {$T_\i$}};  
% % Draw the output layer node
 \pgfmathtruncatemacro{\imax}{{\NeuronList}[2]}
 \path foreach \i in {1,...,\imax}
    {(60mm,{(\imax/2-1/2-\i)*1.5})
    [/utils/exec=\pgfmathsetmacro{\myedgelabel}{{\OutputList}[\i-1]}] 
        node[layer 3] (D-\i)    {$D_\i$}};  
% edges
  \path[nodes={text=red,font=\small\sffamily},->] 
   (P-1) edge["1"] (T-1)
   (P-1) edge["4"' {pos=0.2}] (T-2) %<- a prime swaps the label position
   (P-2) edge["2" {pos=0.15}] (T-2) %<- with pos you can control the position
   (P-3) edge["2"' {pos=0.3}] (T-1)
   (P-3) edge["3"] (P-2)
   (T-1) edge["2"] (D-1)
   (T-1) edge["3"] (D-2)
   (D-1) edge["2"] (D-2)
   (T-2) edge["1"] (D-2)
   (T-2) edge["3"] (D-3);
% extra 
  \begin{scope}[node distance=0pt,nodes={font=\small\sffamily,color=blue}]
   \node[above=of P-1]{(pure)};   
   \node[below=of P-3]{(pure)};   
   \node[below=of el3-2]{(pure)};   
  \end{scope}
\end{tikzpicture}
\end{document}  

在此处输入图片描述

相关内容