从 csv 数据生成 x 轴并合并字节

从 csv 数据生成 x 轴并合并字节

我尝试用 绘制一些.csv-Files pgfplots。通常这没有问题,但在这种情况下,.csv我获得的 -Files 不包含 x 轴的数据。我如何根据 -File 中的行数生成一个递增的数字.csv?在下面的 MWE 中,我手动将其添加到数据中。

除此之外,我获取 y 轴的数据作为两个 8 位值(高字节和低字节),并需要像 一样将它们组合起来value=highbyte*256+lowbyte。我该如何设置呢?


\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.14}

\usepackage{filecontents}
\begin{filecontents*}{test.csv}
M1,M2,M3
0,3,50
1,3,51
2,3,49
3,3,50
4,3,48
5,3,52
6,3,50
7,3,51
8,3,49
9,3,50
10,3,48
11,3,52
12,3,48
13,3,50
14,3,51
15,3,49
16,3,48
17,3,51
18,3,49
19,3,52
20,3,50
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[ymin=0, xmin=0]
\addplot table [x index=0, y index=2, col sep=comma]{test.csv};
\end{axis}
\end{tikzpicture}

\end{document}

答案1

如果表中只有最后两列,则可以使用

\addplot table [
  x expr=\coordindex,
  y expr=256*\thisrowno{0}+\thisrowno{1},% or y expr=256*\thisrow{M2}+\thisrow{M3},
  col sep=comma
]{test.csv};

对于以下示例,我将其更改ymin780

在此处输入图片描述

代码:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}

\usepackage{filecontents}
\begin{filecontents*}{test.csv}
M2,M3
3,50
3,51
3,49
3,50
3,48
3,52
3,50
3,51
3,49
3,50
3,48
3,52
3,48
3,50
3,51
3,49
3,48
3,51
3,49
3,52
3,50
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=780, xmin=0]
\addplot table [
  x expr=\coordindex,
  y expr=256*\thisrowno{0}+\thisrowno{1},% or y expr=256*\thisrow{M2}+\thisrow{M3},
  col sep=comma
]{test.csv};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容