使用 pgfplots 绘制 CDF 的奇怪行为

使用 pgfplots 绘制 CDF 的奇怪行为

我正在使用我找到的一些代码这里我对其进行了一些修改,以绘制离散随机变量的累积分布函数。

第一个图效果很好,但所有后续图的左上角都会出现一个神秘的点,即使生成下一个图的代码完全相同。我想了解确切原因。

这是一个简单的例子:

\documentclass{article}

\usepackage{pgfplots, pgfplotstable}
\usepackage{tikz}

\makeatletter
\long\def\ifnodedefined#1#2#3{%
    \@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}

\pgfplotsset{
    cdf/.style={
    scatter,
    scatter/@pre marker code/.code={
            \ifnodedefined{marker}{
            \pgfpointdiff{\pgfpointanchor{marker}{center}}%
             {\pgfpoint{0}{0}}%
             \ifdim\pgf@y>0pt
                \tikzset{options/.style={mark=*}}
                \draw plot [mark=*,mark options={fill=white}] coordinates {(marker-|0,0)};
             \else
                \ifdim\pgf@y<0pt
                    \tikzset{options/.style={mark=*,fill=white}}
                    \draw plot [mark=*] coordinates {(marker-|0,0)};
                \else
                    \tikzset{options/.style={mark=none}}
                \fi
             \fi
        }
                {
            \tikzset{options/.style={mark=none}}        
        }
        \coordinate (marker) at (0,0);
        \begin{scope}[options]
    },
    scatter/@post marker code/.code={\end{scope}}
    }
}

\begin{document}

\begin{center}
\begin{tikzpicture}[scale = 0.7]
\begin{axis}[
    clip=false,
    jump mark left,
    ymin=0,ymax=1,
    xmin=-3, xmax=6,
    every axis plot/.style={very thick},
    cdf,
    table/create on use/cumulative distribution/.style={
        create col/expr={\pgfmathaccuma + \thisrow{f(x)}}   
    }
]
\addplot [blue] table [y=cumulative distribution]{
x f(x)
-3 0
-2 1/6
0 1/6
2 1/3
4 1/6
5 1/6
6 0
};
\end{axis}
\end{tikzpicture}
\end{center}

\begin{center}
\begin{tikzpicture}[scale = 0.7]
\begin{axis}[
    clip=false,
    jump mark left,
    ymin=0,ymax=1,
    xmin=-3, xmax=6,
    every axis plot/.style={very thick},
    cdf,
    table/create on use/cumulative distribution/.style={
        create col/expr={\pgfmathaccuma + \thisrow{f(x)}}   
    }
]
\addplot [blue] table [y=cumulative distribution]{
x f(x)
-3 0
-2 1/6
0 1/6
2 1/3
4 1/6
5 1/6
6 0
};
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

以及最终的输出。您可以看到在第二个图的左上角添加了神秘点。

在此处输入图片描述

答案1

我不完全理解代码的作用,但解决方法是取消定义节点marker。为此,我们可以使用以下代码有没有办法忘记 TikZ 图片之间的节点名称?并调用:

\aeundefinenode{marker}%

在下一个之前tikzpicture。根据 Paul Gaborit 的建议,为了更像tikz,我们可以用以下样式cdf init来调用它:每个 \addplot

cdf init/.code={\aeundefinenode{marker}}

在此处输入图片描述

笔记:

代码:

\documentclass{article}

\usepackage{pgfplots, pgfplotstable}
\usepackage{tikz}

\makeatletter
\long\def\ifnodedefined#1#2#3{%
  \@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
%% https://tex.stackexchange.com/questions/178778/is-there-a-way-to-forget-node-names-between-tikz-pictures (simplified version)
\def\aeundefinenode#1{%%
  \ifnodedefined{#1}{\global\expandafter\let\csname pgf@sh@ns@#1\endcsname\relax}{}%
}

\pgfplotsset{
    cdf init/.code={\aeundefinenode{marker}},
    cdf/.style={
    scatter,
    scatter/@pre marker code/.code={
        \ifnodedefined{marker}{
            \pgfpointdiff{\pgfpointanchor{marker}{center}}%
             {\pgfpoint{0}{0}}%
             \ifdim\pgf@y>0pt
                \tikzset{options/.style={mark=*}}
                \draw plot [mark=*,mark options={fill=white}] coordinates {(marker-|0,0)};
             \else
                \ifdim\pgf@y<0pt
                    \tikzset{options/.style={mark=*,fill=white}}
                    \draw plot [mark=*] coordinates {(marker-|0,0)};
                \else
                    \tikzset{options/.style={mark=none}}
                \fi
             \fi
        }
                {
            \tikzset{options/.style={mark=none}}        
        }
        \coordinate (marker) at (0,0);
        \begin{scope}[options]
    },
    scatter/@post marker code/.code={\end{scope}}
    }
}

\makeatother

\begin{document}

\begin{tikzpicture}[scale = 0.7]
\begin{axis}[
    clip=false,
    jump mark left,
    ymin=0,ymax=1,
    xmin=-3, xmax=6,
    every axis plot/.style={very thick},
    cdf,
    table/create on use/cumulative distribution/.style={
        create col/expr={\pgfmathaccuma + \thisrow{f(x)}}   
    }
]
    \addplot [cdf init, blue] table [y=cumulative distribution]{%  <---------- Added 'cdf init'.
    x f(x)
    -3 0
    -2 1/6
    0 1/6
    2 1/3
    4 1/6
    5 1/6
    6 0
    };
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[scale = 0.7]
\begin{axis}[
    clip=false,
    jump mark left,
    ymin=0,ymax=1,
    xmin=-3, xmax=6,
    every axis plot/.style={very thick},
    cdf,
    table/create on use/cumulative distribution/.style={
        create col/expr={\pgfmathaccuma + \thisrow{f(x)}}   
    }
]
    \addplot [cdf init, red] table [y=cumulative distribution]{%  <---------- Added 'cdf init'.
    x f(x)
    -3 0
    -2 1/6
    0 1/6
    2 1/3
    4 1/6
    5 1/6
    6 0
    };
\end{axis}
\end{tikzpicture}

\end{document}

相关内容