在 Tikzpicture 中定位图形

在 Tikzpicture 中定位图形

我第一次制作 LaTeX 演示文稿,并尝试制作包含图形的流程图,但出于某种原因,图形的位置偏向了其应在位置的右上方。高斯分布应该显示在箭头指向其下方的位置。

滑动

\documentclass[10pt]{beamer}
\usepackage[utf8]{inputenc}

\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usepackage{pgfpages}

%% Defining tikz drawing primitives
\tikzstyle{block} = [rectangle, draw, fill=blue!20, 
    text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{whiteblock} = [rectangle, draw, 
    text width=5em, text centered, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
    minimum height=2em]
\pgfmathdeclarefunction{gauss}{2}{\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}

\begin{document}
\begin{frame}{Frame}
\begin{itemize}
\item Create thresholds in standard normal based on frequencies of categorical
\item Sample a few (5) points for each categorical value in the threshold
\end{itemize}
\begin{tikzpicture}[node distance = 2.5cm, auto]
\node [whiteblock] (cat) {Categorical};
\node [whiteblock, right of=cat] (freq) {Frequencies};
\node [whiteblock, right of=freq] (thresh) {Thresholds};
\node [whiteblock, right of=thresh] (samp) {Sample Continuous};

\node [block, below of=cat] (catr) {A;A;B;C};
\node [block, right of=catr] (freqr) {A: 0.5, B: 0.25, C: 0.25};
\node[inner sep=1pt, right of=freqr](threshr)
    {
\begin{axis}[no markers, domain=-2:2, samples=100, xlabel=\empty, ylabel=\empty,
axis lines*=left,
height=3cm, width=4cm,
xticklabels={A,B,C}, 
ytick=\empty,
enlargelimits=false, clip=false, axis on top,
grid = major]
\addplot [fill=cyan!20, draw=none, domain=-2:0] {gauss(0,1)} \closedcycle;
\addplot [fill=orange!20, draw=none, domain=0:1] {gauss(0,1)} \closedcycle;
\addplot [fill=red!20, draw=none, domain=1:2] {gauss(0,1)} \closedcycle;
\end{axis}
    };
\node [block, right of=threshr] (sampr) {Sample Continuous};

\path [line] (cat) -- (freq);
\path [line] (freq) -- (thresh);
\path [line] (thresh) -- (samp);

\path [line] (freqr) -- (threshr);
\end{tikzpicture}
\end{frame}

\begin{frame}{second}
\end{frame}
\end{document}

我还注意到它当前的方式奇怪地抵消了它下面的幻灯片。

抵消

我应该怎样修复此问题?

答案1

请只问一个问题:-)

作为您图像的起点(我仍然需要处理您的pgfplots图像:-()

\documentclass{beamer}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\usetikzlibrary{arrows.meta,
                chains,
                positioning}
\pgfmathdeclarefunction{gauss}{2}{\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}%

\begin{document}
\begin{frame}{Categorical to Normal}
\begin{itemize}
\item Create thresholds in standard normal based on frequencies of categorical
\item Sample a few (5) points for each categorical value in the threshold
\end{itemize}

\newcommand\GAUSS{\tikz{
\begin{axis}[
xlabel=\empty, ylabel=\empty,
axis lines*=left,
height=3cm, width=4cm,
xticklabels={A,B,C},
ytick=\empty,
enlargelimits=false,
grid = major,
no markers,
domain=-2:2, samples=100,
]
\addplot [fill=cyan!20, draw=none, domain=-2:0] {gauss(0,1)} \closedcycle;
\addplot [fill=orange!20, draw=none, domain=0:1] {gauss(0,1)} \closedcycle;
\addplot [fill=red!20, draw=none, domain=1:2] {gauss(0,1)} \closedcycle;
\end{axis}
            };}

    \begin{tikzpicture}[
        node distance = 6mm and 4mm,
          start chain = A going right,
          start chain = B going right,
        wblock/.style = {draw, minimum size =24mm, align=center, on chain=A, join=by -Stealth},
         block/.style = {draw, rounded corners, fill=blue!30,
                         minimum size =24mm, align=center,
                         on chain=B, join=by -Stealth},
                          ]
\node [wblock] (cat)    {Categorical};
\node [wblock] (freq)   {Frequencies};
\node [wblock] (thresh) {Thresholds};
\node [wblock] (samp)   {Sample\\ Continuous};

\node [block,below=of cat] (catr)   {A;A;B;C};
\node [block] (freqr)               {A: 0.5,\\ B: 0.25,\\ C: 0.25};
\node [block, fill=white, inner sep=0pt,scale=0.4] (threshr)
                                    {\GAUSS};
\node [block] (sampr)               {Sample\\ Continuous};
\end{tikzpicture}
\end{frame}
\end{document}

给出:

在此处输入图片描述

编辑:在节点中包含pgfplots图像时我遇到了难题。这里有一个解决方案,将pgfplot图像定义为特殊节点,并通过机制将样式定义包含在pgfplots代码中append after command={\pgfextra{ ...}}

在此处输入图片描述

新解决方案的代码是:

\documentclass{beamer}%[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{arrows.meta,
                chains,
                positioning}
\pgfmathdeclarefunction{gauss}{2}{\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}%

\begin{document}
\begin{frame}
\frametitle{Categorical to Normal}

 \begin{itemize}
\item Create thresholds in standard normal based on frequencies of categorical
\item Sample a few (5) points for each categorical value in the threshold
\end{itemize}

    \begin{tikzpicture}[
        node distance = 6mm and 4mm,
          start chain = A going right,
          start chain = B going right,
        wblock/.style = {draw, minimum size =24mm, align=center, on chain=A, join=by -Stealth},
         block/.style = {draw, rounded corners, fill=blue!30,
                         minimum size =24mm, align=center,
                         on chain=B, join=by -Stealth},
      pgfblock/.style = {block, fill=none,
                         append after command={\pgfextra{
\begin{axis}[at={(\tikzlastnode.south west)}, shift={(3mm,4mm)},
    width=18mm,
    scale only axis,
    axis lines=left,
    xticklabels={,,A,B,C},
    x tick label style={inner sep=1pt,font=\tiny},
    ytick=\empty,
    axis on top,
    grid,
    no markers,
]
\addplot [fill=cyan!20, draw=none, domain=-2:0]     {gauss(0,1)} \closedcycle;
\addplot [fill=orange!20, draw=none, domain=0:1]    {gauss(0,1)} \closedcycle;
\addplot [fill=red!20, draw=none, domain=1:2]   {gauss(0,1)} \closedcycle;
\end{axis}
                    }}}
                          ]
\node [wblock] (cat)    {Categorical};
\node [wblock] (freq)   {Frequencies};
\node [wblock] (thresh) {Thresholds};
\node [wblock] (samp)   {Sample\\ Continuous};

\node [block,below=of cat] (catr)   {A;A;B;C};
\node [block] (freqr)               {A: 0.25,\\ B: 0.5,\\ C: 0.25};
\node [pgfblock] (threshr)          {};
\node [block] (sampr)               {Sample\\ Continuous};
\end{tikzpicture}
\end{frame}
\end{document}

我确信更简单的方法是单独绘制高斯图(使用standalone)并将其作为pdf图像包含在内:

\node [block,below=of cat] (catr)   {A;A;B;C};
\node [block] (freqr)               {A: 0.5,\\ B: 0.25,\\ C: 0.25};
\node [block,fill=none] (threshr)   {\includegraphics[width=\linewidth]{GAUSS}}; 
\node [block] (sampr)               {Sample\\ Continuous};

其中 GAUSS 是pdf例如由以下程序生成的图像文件:

%%%% GAUSS
\documentclass[margin=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16,width=22mm,height=22mm}
\pgfmathdeclarefunction{gauss}{2}{\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}}%

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
    scale only axis,
    axis lines=left,
    xticklabels={,,A,B,C},
    x tick label style={inner sep=1pt},
    ytick=\empty,
    axis on top,
    grid,
    no markers,
            ]
\addplot [fill=cyan!20,     draw=none, domain=-2:0] {gauss(0,1)} \closedcycle;
\addplot [fill=orange!20,   draw=none, domain= 0:1] {gauss(0,1)} \closedcycle;
\addplot [fill=red!20,      draw=none, domain= 1:2] {gauss(0,1)} \closedcycle;
\end{axis}
    \end{tikzpicture}
\end{document}

相关内容