动态重新标记 y 轴?

动态重新标记 y 轴?

如何以独立于具体数据的灵活方式将下图中的 y 轴从 0-max(此处为 0-3)重新标记为 0-1?例如,是否可以访问绘图期间发生的最大 y 值并使用它来确定 y 标签 1 的位置?

这个问题继续讨论如何在 LaTex 环境中绘制离散数的累积分布函数 (CDF)?

在此处输入图片描述

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\makeatletter
\long\def\ifnodedefined#1#2#3{%
    \@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}

\pgfplotsset{
    discontinuous/.style={
    scatter,
    scatter/@pre marker code/.code={
        \ifnodedefined{marker}{
            \pgfpointdiff{\pgfpointanchor{marker}{center}}%
             {\pgfpoint{0}{0}}%
             \ifdim\pgf@y>0pt
                \tikzset{options/.style={mark=*, fill=white}}
                \draw [densely dashed,blue] (marker-|0,0) -- (0,0);
                \draw plot [mark=*] coordinates {(marker-|0,0)};
             \else
                \tikzset{options/.style={mark=none}}
             \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}
\begin{axis}[
    clip=false,
    jump mark left,
    ymin=0,ymax=3.5,
    xmin=14,xmax=35,
    xlabel={income},
    ylabel={cumulative distribution},
    every axis plot/.style={very thick},
    discontinuous,
    table/create on use/cumulative distribution/.style={
        create col/expr={\pgfmathaccuma + \thisrow{f(x)}}   
    }
]
\addplot [red] table [y=cumulative distribution]{
P(x) f(x)
14   0
15  1/5
18  2/5
25 3/5
31 4/5
33 1
35 0
};
\end{axis}
\end{tikzpicture}
\end{document}

答案1

使用pgfplots:读取数据,计算图表您可以计算累积百分比,将其添加为额外列,然后绘制该列。

梅威瑟:

\documentclass[border=2mm]{standalone}

\usepackage{pgfplots}
\usepackage{pgfplotstable}

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

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

\makeatother

\pgfplotstableread{
P(x)    f(x)
14  0
15  1/5
18  2/5
25  3/5
31  4/5
33  1
35  0
}\datatable
% Calculate the sum of the y column
\pgfmathsetmacro\pgfplotstablesum{0}
\pgfplotstableforeachcolumnelement{f(x)}\of\datatable\as\yvalue{
    \pgfmathsetmacro\pgfplotstablesum{\pgfplotstablesum+\yvalue}
}
% Define a "virtual column" that calculates the cumulative percentage on the fly
\pgfplotstableset{
    create on use/cumulative percentage/.style={
        create col/expr={\pgfmathaccuma + \thisrow{f(x)}/\pgfplotstablesum}
    }
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    clip=false,
    jump mark left,
    ymin=0,ymax=1.0,
    xmin=14,xmax=35,
    xlabel={income},
    ylabel={cumulative distribution},
    every axis plot/.style={very thick},
    discontinuous
]
\addplot [red] table [y=cumulative percentage]{\datatable};
\end{axis}
\end{tikzpicture}

\end{document}

结果:

在此处输入图片描述

相关内容