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}

相關內容