pgfplots - 絶対値と相対値の両方を表示する

pgfplots - 絶対値と相対値の両方を表示する

私のプロットでは、絶対値の横に相対値を表示しようとしています。たとえば、右端の列では、9.02*10^6 のすぐ下に 110 と表示したいです。もう 1 つのオプションは、右側の軸に 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使用してこれらをオンザフライで生成し、キー を使用してデータのテキストを目盛りラベルとして使用できます。このようにして、ラベルを変更することにした場合、1 か所を変更するだけで済みます。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}

関連情報