在 Tikz 中绘制自定义图并设置间距

在 Tikz 中绘制自定义图并设置间距

我实际上正在努力绘制使用 MS powerpoint 制作的自定义图。我从之前的问题中得到了很多帮助,但我仍然无法解决放置问题和各种其他错误,以便制作我拥有的图形的副本。

我希望得到的印象是:

在此处输入图片描述

我尝试编写代码:

\documentclass[border=3pt,tikz]{standalone}
\usepackage{tikz}
\usepackage{etoolbox} % for \ifnumcomp
\usepackage{listofitems} % for \readlist to create arrays

\tikzset{>=latex} % for LaTeX arrow head
\colorlet{mywhite}{white!80!black}
\colorlet{myred}{red!80!black}
\colorlet{myblue}{blue!80!black}
\colorlet{mygreen}{green!60!black}
\colorlet{mydarkred}{myred!40!black}
\colorlet{mydarkblue}{myblue!40!black}
\colorlet{mydarkgreen}{mygreen!40!black}
\tikzstyle{node}=[very thick,circle,draw=myblue,minimum size=22,inner sep=0.5,outer sep=0.6]
\tikzstyle{connect}=[->,thick,mydarkblue,shorten >=1]
\tikzset{ % node styles, numbered for easy mapping with \nstyle
  node 1/.style={node,mydarkgreen,draw=mywhite,fill=mywhite!25},
  node 2/.style={node,mydarkblue,draw=mywhite,fill=mywhite!20},
  node 3/.style={node,mydarkred,draw=mywhite,fill=mywhite!20},
}
\def\nstyle{int(\lay<\Nnodlen?min(2,\lay):3)} % map layer number onto 1, 2, or 3

\begin{document}


% NEURAL NETWORK
\begin{tikzpicture}[x=2.4cm,y=1.2cm]

  \readlist\Nnod{4,3,2} % array of number of nodes per layer
  \readlist\Nstr{n,m,k} % array of string number of nodes per layer
  \readlist\Cstr{x,h^{(\prev)},y} % array of coefficient symbol per layer
  \def\yshift{0.55} % shift last node for dots
  
  % LOOP over LAYERS
  \foreachitem \N \in \Nnod{
    \def\lay{\Ncnt} % alias of index of current layer
    \pgfmathsetmacro\prev{int(\Ncnt-1)} % number of previous layer
    \foreach \i [evaluate={\c=int(\i==\N); \y=\N/2-\i-\c*\yshift;
                 \x=\lay; \n=\nstyle;
                 \index=(\i<\N?int(\i):"\Nstr[\n]");}] in {1,...,\N}{ % loop over nodes
      % NODES
      \node[node \n] (N\lay-\i) at (\x,\y) {$\strut\Cstr[\n]_{\index}$};
      
      % CONNECTIONS
      \ifnumcomp{\lay}{>}{1}{ % connect to previous layer
        \foreach \j in {1,...,\Nnod[\prev]}{ % loop over nodes in previous layer
          \draw[white,line width=1.2,shorten >=1] (N\prev-\j) -- (N\lay-\i);
          \draw[connect] (N\prev-\j) -- (N\lay-\i);
        }
        \ifnum \lay=\Nnodlen
          \draw[connect] (N\lay-\i) --++ (0.5,0); % arrows out
        \fi
      }{
        \draw[connect] (0.5,\y) -- (N\lay-\i); % arrows in
      }
      
    }
    \path (N\lay-\N) --++ (0,1+\yshift) node[midway,scale=1.6] {$\vdots$}; % dots
  }
  
  % LABELS
  \node[above=3,align=center,mydarkgreen] at (N1-1.90) {Input\\[-0.2em]layer};
  \node[above=2,align=center,mydarkblue] at (N2-1.90) {Hidden\\[-0.2em]layers};
  \node[above=3,align=center,mydarkred] at (N\Nnodlen-1.90) {Output\\[-0.2em]layer};
  
  
  \begin{scope}[scale=0.75] 
    \draw[blue!50!black] plot[domain={-1.5:5}] (\x,{sin(\x^2 r)});
    \draw[blue!50!black, densely dotted] (-2,0) -- (5,0);
    \draw[black, densely dashed] (0,0) 
        -- ++(0,-2) node[below] {$x_1$} coordinate[pos=0] (A);
    \draw[black, densely dashed] (0.5,0) 
        -- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
    \draw[black, densely dashed] (1.0,0) 
    -- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
    \draw[black, densely dashed] (1.5,0) 
    -- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
    \draw[black, densely dashed] (2.0,0) 
    -- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);

    \draw[thick, <->] (-2,4) 
        -- node[left, midway, anchor=south, rotate=90, font=\bfseries] (Y) {$f(x)$} (-2,-2)
        -- node[below, pos=1, font=\bfseries] {Quad. points} (5,-2);
  \end{scope}
  
\end{tikzpicture}


\end{document}

但是它会生成以下图像:这并不好。

在此处输入图片描述

任何帮助或建议都将不胜感激。

答案1

我假设您是从其他来源复制此代码的,因为其中存在许多不一致之处。无论如何,您可以在scope绘制曲线的位置添加偏移。请参阅下面的代码。

在 tikz 中绘制自定义图并设置间距

\documentclass[border=3pt,tikz]{standalone}
\usepackage{tikz}
\usepackage{etoolbox} % for \ifnumcomp
\usepackage{listofitems} % for \readlist to create arrays

\tikzset{>=latex} % for LaTeX arrow head
\colorlet{mywhite}{white!80!black}
\colorlet{myred}{red!80!black}
\colorlet{myblue}{blue!80!black}
\colorlet{mygreen}{green!60!black}
\colorlet{mydarkred}{myred!40!black}
\colorlet{mydarkblue}{myblue!40!black}
\colorlet{mydarkgreen}{mygreen!40!black}
\tikzstyle{node}=[very thick,circle,draw=myblue,minimum size=22,inner sep=0.5,outer sep=0.6]
\tikzstyle{connect}=[->,thick,mydarkblue,shorten >=1]
\tikzset{ % node styles, numbered for easy mapping with \nstyle
  node 1/.style={node,mydarkgreen,draw=mywhite,fill=mywhite!25},
  node 2/.style={node,mydarkblue,draw=mywhite,fill=mywhite!20},
  node 3/.style={node,mydarkred,draw=mywhite,fill=mywhite!20},
}
\def\nstyle{int(\lay<\Nnodlen?min(2,\lay):3)} % map layer number onto 1, 2, or 3

\begin{document}


% NEURAL NETWORK
\begin{tikzpicture}[x=2.4cm,y=1.2cm]

  \readlist\Nnod{4,3,2} % array of number of nodes per layer
  \readlist\Nstr{n,m,k} % array of string number of nodes per layer
  \readlist\Cstr{x,h^{(\prev)},y} % array of coefficient symbol per layer
  \def\yshift{0.55} % shift last node for dots
  
  % LOOP over LAYERS
  \foreachitem \N \in \Nnod{
    \def\lay{\Ncnt} % alias of index of current layer
    \pgfmathsetmacro\prev{int(\Ncnt-1)} % number of previous layer
    \foreach \i [evaluate={\c=int(\i==\N); \y=\N/2-\i-\c*\yshift;
                 \x=\lay; \n=\nstyle;
                 \index=(\i<\N?int(\i):"\Nstr[\n]");}] in {1,...,\N}{ % loop over nodes
      % NODES
      \node[node \n] (N\lay-\i) at (\x,\y) {$\strut\Cstr[\n]_{\index}$};
      
      % CONNECTIONS
      \ifnumcomp{\lay}{>}{1}{ % connect to previous layer
        \foreach \j in {1,...,\Nnod[\prev]}{ % loop over nodes in previous layer
          \draw[white,line width=1.2,shorten >=1] (N\prev-\j) -- (N\lay-\i);
          \draw[connect] (N\prev-\j) -- (N\lay-\i);
        }
        \ifnum \lay=\Nnodlen
          \draw[connect] (N\lay-\i) --++ (0.5,0); % arrows out
        \fi
      }{
        \draw[connect] (0.5,\y) -- (N\lay-\i); % arrows in
      }
      
    }
    \path (N\lay-\N) --++ (0,1+\yshift) node[midway,scale=1.6] {$\vdots$}; % dots
  }
  
  % LABELS
  \node[above=3,align=center,mydarkgreen] at (N1-1.90) {Input\\[-0.2em]layer};
  \node[above=2,align=center,mydarkblue] at (N2-1.90) {Hidden\\[-0.2em]layers};
  \node[above=3,align=center,mydarkred] at (N\Nnodlen-1.90) {Output\\[-0.2em]layer};
  
  
  \begin{scope}[scale=0.75,xshift=18cm,yshift=-2cm] % <---- HERE
    \draw[blue!50!black] plot[domain={-1.5:5}] (\x,{sin(\x^2 r)});
    \draw[blue!50!black, densely dotted] (-2,0) -- (5,0);
    \draw[black, densely dashed] (0,0) 
        -- ++(0,-2) node[below] {$x_1$} coordinate[pos=0] (A);
    \draw[black, densely dashed] (0.5,0) 
        -- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
    \draw[black, densely dashed] (1.0,0) 
    -- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
    \draw[black, densely dashed] (1.5,0) 
    -- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);
    \draw[black, densely dashed] (2.0,0) 
    -- ++(0,-2) node[below] {$x_2$} coordinate[pos=0] (B);

    \draw[thick, <->] (-2,4) 
        -- node[left, midway, anchor=south, rotate=90, font=\bfseries] (Y) {$f(x)$} (-2,-2)
        -- node[below, pos=1, font=\bfseries] {Quad. points} (5,-2);
  \end{scope}
  
\end{tikzpicture}


\end{document}

附注:您只能使用tikzset而不能使用,tikzstyle但后者现已被弃用。

相关内容