아래 그림과 같은 플롯의 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: 데이터 읽기, 다이어그램 계산누적 백분율을 계산하고 이를 추가 열로 추가한 다음 해당 열을 그릴 수 있습니다.
MWE:
\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}
결과: