我有一张包含三行的表格。每行由一对“输入”和“输出”值组成。此外,这些对(w1、w2、w3)有三个时间测量值。参数“输入”和“输出”与以下任务无关。
这是描述的表格:
w1,w2,w3,in,out
0.023848,0.00289,0.001991,132.0,269.4773
0.004663,0.002165,0.002014,239.0,462.0121
0.003875,0.002332,0.002027,152.0,305.4651
我的目标是在 y 轴上打印这九个汇总的时间测量值,并在 x 轴上打印每个值 (1,...,9) 的计数器。为了阐明我的意思,我在 Excel 中创建了一个示例:
作为起点,我在“axis”环境中使用了以下函数。现在的问题是:
如何使数据与参数匹配X和是我的职责是什么?
...
\addplot[aqua, thick, dotted] table [x=???, y=???, col sep=comma] {data.csv};
\addlegendentry{Device 1}
...
答案1
可能有更优雅的方法,但下面的代码似乎有效。对于您的实际情况,您可以忽略filecontents
开头的内容,这些环境所做的就是将其内容写入指定的文件,这只是使示例自足的一种方式。第二个文件是您提供的文件的副本,修改了几个值。
代码有一些注释,但大体要点是我定义了一个新的宏\updatedatatable
,它读取一个 csv 文件,重新排列数据并将其保存到\datatable
,您可以使用它进行绘图\addplot table {\datatable};
。因此,在每次绘制新图之前,调用\updatetabletable{file.csv}
以更新内容\datatable
。如果需要访问所有各种表格,可以轻松修改它以使用不同的表格名称。
\begin{filecontents*}{data.csv}
w1,w2,w3,in,out
0.023848,0.00289,0.001991,132.0,269.4773
0.004663,0.002165,0.002014,239.0,462.0121
0.003875,0.002332,0.002027,152.0,305.4651
\end{filecontents*}
\begin{filecontents*}{data2.csv}
w1,w2,w3,in,out
0.023848,0.00289,0.001991,132.0,269.4773
0.004663,0.007165,0.002014,239.0,462.0121
0.003875,0.002332,0.008027,152.0,305.4651
\end{filecontents*}
\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
% probably not strictly necessary, but a style for making a column of x-values
\pgfplotstableset{
create on use/x/.style={create col/set list={1,...,9}}
}
% macro that takes one argument: a file name
\newcommand\updatedatatable[1]{
% clean slate: clear out \datatable and \tmptable
\pgfplotstableclear{\datatable}
\pgfplotstableclear{\tmptable}
% read the file into \tmptable
\pgfplotstableread[col sep=comma]{#1}{\tmptable}
% create a new table with nine rows, containing the x-column defined above
\pgfplotstablenew[columns={x}]{9}{\datatable}
% create a new column for the y-values
\pgfplotstablecreatecol[
% code defining the cell contents
create col/assign/.code={
% \pgfplotstablerow is the row number (N) in our new datatable
% we extract the data from the original table by indexing into rows and columns
% \pgfmathtruncatemacro calculates the result and removes the decimal part, saving an integer
% calculate the column index as mod(N, 3), save result in \tmpcol
\pgfmathtruncatemacro{\tmpcol}{mod(\pgfplotstablerow, 3)}
% calculate row index as floor(N/3). The truncation does the same as floor
\pgfmathtruncatemacro{\tmprow}{\pgfplotstablerow/3}
% extract the value from \tmptable, result saved in \pgfplotsretval
\pgfplotstablegetelem{\tmprow}{[index]\tmpcol}\of\tmptable
\edef\entry{\pgfplotsretval}
% put the entry into the table
\pgfkeyslet{/pgfplots/table/create col/next content}\entry
}
% the column is named y, and saved into \datatable
]{y}\datatable
}
\begin{document}
\begin{tikzpicture}
% read in the first file, plot \datatable
\updatedatatable{data.csv}
\begin{axis}
\addplot[mark=o, mark options={solid}, thick, dotted] table {\datatable};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
% read in the second file. from now on \datatable has the data from data2.csv
\updatedatatable{data2.csv}
\begin{axis}
\addplot[mark=o, mark options={solid}, thick, dotted] table {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}