pgfplots - 절대값과 상대값을 모두 표시합니다.

pgfplots - 절대값과 상대값을 모두 표시합니다.

내 플롯에서는 절대값 옆에 상대값을 표시하려고 합니다. 예를 들어, 가장 오른쪽 열의 경우 9.02*10^6 바로 아래에 110이라고 말하고 싶습니다. 또 다른 옵션은 1에서 110까지의 "속도 향상"이 있는 오른쪽 축입니다.

또한 막대가 x축 선에서 바로 시작되도록 플롯을 아래로 어떻게 이동할 수 있습니까?

\begin{tikzpicture}
\begin{axis}[
    ybar,
        scale=0.9,
        axis x line= bottom,
        axis y line = left,
        x post scale = 1.5,
        enlargelimits=0.15,
        anchor=west,
        ylabel=Rays/second,
        symbolic x coords={Original, Linear Traverser, MT Linear Traverser, CPU kd-tree, GPU kd-tree},
        xtick=data,
        nodes near coords, 
        nodes near coords align={vertical},
        x tick label style={rotate=45,anchor=east, xshift=-0.1em, yshift=-0.01em},
]
\addplot coordinates {(Original, 81685) (Linear Traverser, 506326) (MT Linear Traverser, 1754330)
    (CPU kd-tree, 1873746) (GPU kd-tree, 9023256)};
\end{axis}
\end{tikzpicture}

예시 이미지

답변1

우선 좌표 목록이 아닌 테이블 형식으로 데이터를 제공하는 것이 좋습니다. 이를 통해 값을 조작하기가 훨씬 쉬워지고 파일의 데이터를 사용할 수 있습니다. 또한 일반적으로 x 위치에 symbolic x coords. 대신 숫자 인덱스를 사용하는 것을 선호합니다 . 옵션 x expr=\coordindex에서 \addplot table [...]이를 즉시 생성 할 수 있으며 , 키를 사용하여 데이터의 텍스트를 눈금 레이블로 사용할 수 있습니다 xticklabels from file. 이렇게 하면 레이블을 변경하기로 결정한 경우 단일 위치에서만 변경하면 됩니다.

\datatable명령을 사용하여 호출된 매크로로 데이터 테이블을 읽은 경우 다음을 \pgfplotstableread사용하여 상대 값을 포함하는 새 열을 생성할 수 있습니다.

% Get base value
\pgfplotstablegetelem{0}{Value}\of\datatable
% Calculate relative values
\pgfplotstablecreatecol[
    create col/expr={
        \thisrow{Value}/\pgfplotsretval*100
    }
]{Relative}{\datatable}

nodes near coords절대값 외에 이러한 값을 에서 사용할 수 있도록 하려면 키를 사용할 수 있습니다 visualization depends on=\thisrow{Relative} \as \relativevalue. 그런 다음 를 사용하여 값에 액세스할 수 있습니다 \relativevalue. 안타깝게도 이는 테이블 매크로가 아닌 데이터 파일에서 직접 플롯을 생성한 경우에만 작동합니다. 여기서 가장 쉬운 방법은 ( 텍스트 레이블의 공백 때문에 필요함)을 Relative사용하여 새 데이터 테이블(새 열이 포함됨)을 임시 파일에 저장하는 것입니다.\pgfplotstablesave[col sep=comma]{\datatable}{temptable.txt}col sep=comma

그런 다음 두 값을 모두 표시하도록 설정할 수 있습니다 nodes near coords. 나는 siunitx숫자를 반올림하고 형식을 지정했습니다.

nodes near coords={%
    \pgfmathfloattofixed{\pgfplotspointmeta}%
    \num[round-mode=figures, round-precision=2]{\pgfmathresult}\\%
    \SI[round-mode=figures, round-precision=2]{\relativevalue}{\percent}%
}

플롯이 y축에서 시작되도록 하려면 를 설정할 수 있습니다 enlarge y limits=upper, ymin=0.

그 결과는 다음과 같습니다.

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.7}
\usepackage{siunitx}

\begin{document}

\pgfplotstableread{
Name Value
Original 81685
{Linear Traverser} 506326
{MT Linear Traverser} 1754330
{CPU kd-tree} 1873746
{GPU kd-tree} 9023256
}\datatable

% Get base value
\pgfplotstablegetelem{0}{Value}\of\datatable
% Calculate relative values
\pgfplotstablecreatecol[
    create col/expr={
        \thisrow{Value}/\pgfplotsretval*100
    }
]{Relative}{\datatable}

\pgfplotstablesave[col sep=comma]{\datatable}{temptable.txt}

\begin{tikzpicture}
\begin{axis}[
        ybar,
        scale=0.9,
        axis x line= bottom,
        axis y line = left,
        x post scale = 1.5,
        enlargelimits=0.15,
        enlarge y limits=upper, ymin=0,
        anchor=west,
        ylabel=Rays/second,
        xticklabels from table={\datatable}{Name},
        xtick=data, ytick=\empty,
        visualization depends on=\thisrow{Relative}\as\relativevalue,
        nodes near coords={%
            \pgfmathfloattofixed{\pgfplotspointmeta}%
            \num[round-mode=figures, round-precision=2]{\pgfmathresult}\\%
            \SI[round-mode=figures, round-precision=2]{\relativevalue}{\percent}%
        },
        nodes near coords align={vertical},
        every node near coord/.append style={align=center},
        x tick label style={rotate=45,anchor=east, xshift=-0.1em, yshift=-0.01em},
]
\addplot [fill=gray] table [x expr=\coordindex, col sep=comma] {temptable.txt};
\end{axis}
\end{tikzpicture}
\end{document}

관련 정보