我正在嘗試實現一些雄心勃勃的目標,但我一直致力於如何使用pgfplots
和操作表資料pgfplotstable
。
我有一個數據文件,其中包含一定數量的不同數據集中的許多不同工具的基準測試結果。該文件具有以下形式:
datasets tool1 tool2 tool3 tool4 tool5 tool6
dataset1 1 2 3 4 5 6
dataset2 1 2 3 4 5 6
dataset3 1 2 3 4 5 6
dataset4 1 2 3 4 5 6
dataset5 1 2 3 4 5 6
dataset6 1 2 3 4 5 6
dataset7 1 2 3 4 5 6
dataset8 1 2 3 4 5 6
也就是說,工具放置在不同的列上,資料集放置在不同的行上。我想將這些數據繪製在相同形狀的表格中,其中每行的長條圖,顯示不同工具在每行資料集上的計時。
因此,表格的第一列應報告資料集的名稱,第二列應包含長條圖。
我遇到的問題:
- 如何迭代文件的行以產生表?
- 我可以想像使用 進行迭代
\foreach
,但在從文件的單一給定行獲取條形圖時遇到問題。
- 我可以想像使用 進行迭代
- 如何從每一行產生一個長條圖?
我不知道如何嘲笑我的預期結果。它就像是這例如,使用長條圖而不是雨圖,並且無需對第一列上的行名稱進行硬編碼。
有什麼建議嗎?
答案1
如果您的檔案名稱為例如。mydata.csv
, 您可以使用
\pgfplotstabletranspose[colnames from=datasets,input colnames to=datasets]{\data}{mydata.csv}
範例1:
\begin{filecontents*}{mydata.csv}
datasets tool1 tool2 tool3 tool4 tool5 tool6
dataset1 2 3 4 5 6 7
dataset2 1 2 3 4 5 6
dataset3 1 2 3 4 5 6
dataset4 1 2 3 4 5 6
dataset5 1 2 3 4 5 6
dataset6 1 2 3 4 5 6
dataset7 1 2 3 4 5 6
dataset8 1 2 3 4 5 6
\end{filecontents*}
\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14}
\begin{document}
\pgfplotstabletranspose[colnames from=datasets,input colnames to=datasets]{\data}{mydata.csv}
\foreach \dataset in {1,...,8}{%
\noindent Dataset \dataset\qquad
\begin{tikzpicture}[baseline={([yshift=-\baselineskip]p.north)}]
\begin{axis}[
height=4cm,
width=\axisdefaultwidth,
ybar,
xtick=data,
xticklabels from table={\data}{datasets},
name=p
]
\addplot table[x expr=\coordindex,y index=\dataset]{\data};
\end{axis}
\end{tikzpicture}%
\par
}%
\end{document}
範例2:
\begin{filecontents*}{mydata.csv}
datasets tool1 tool2 tool3 tool4 tool5 tool6
dataset1 2 3 4 5 6 7
dataset2 1 2 3 4 5 6
dataset3 1 2 3 4 5 6
dataset4 1 2 3 4 5 6
dataset5 1 2 3 4 5 6
dataset6 1 2 3 4 5 6
dataset7 1 2 3 4 5 6
dataset8 1 2 3 4 5 6
\end{filecontents*}
\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\pgfplotstabletranspose[colnames from=datasets,input colnames to=datasets]{\data}{mydata.csv}
\begin{axis}[
width=\textwidth,
ybar,
bar width=2pt,
xtick=data,
xticklabels from table={\data}{datasets},
legend pos=north west,
cycle list name=color list,
every axis plot/.append style=fill
]
\pgfplotsinvokeforeach {1,...,8}
{
\addplot table[x expr=\coordindex,y index=#1]{\data};
\addlegendentry{dataset#1}
}
\end{axis}
\end{tikzpicture}
\end{document}
結果: