我有一个 csv 文件,格式为
100000000,0.0565325,-122.797,0.9984,-32.5345,0.9984,-32.5345,0.0565325,-122.271
101000000,0.0570362,-123.125,0.998372,-32.8592,0.998372,-32.8592,0.0570362,-122.593
102000000,0.0575381,-123.452,0.998343,-33.1838,0.998343,-33.1838,0.0575381,-122.915
103000000,0.0580382,-123.779,0.998314,-33.5085,0.998314,-33.5085,0.0580382,-123.237
并使用 绘制数据pgfplots
。但是我想“操纵”这些值
- 将第一列除以 $10^9$ (Hz->GHz)
- 执行“20log(abs(x))”,因为 x 以线性形式给出并且需要以 dB 为单位绘制。
我在 pgfplots 文档中发现了 4 次出现,calc
但这些案例并不处理读取信息。
我知道情节不匹配。
答案1
欢迎使用 TeX.SX!您可以使用选项x expr
和y expr
对从表中读取的数据(来自文件或内联)执行计算。
如果您的数据没有列名,您可以使用\thisrowno
来访问相关列中的数据(\thisrowno{0}
描述第一列)。如果列有名称,您可以使用 来访问相关数据中的数据\thisrow{<column name>}
。
因此,例如,您可以写入y expr={20*log10(abs(\thisrowno{1}))}
对 CSV 数据第二列的值应用 20 log(abs(x))。
我不确定以下内容是否是您想要的,因为您没有提供最小工作示例(MWE),但它至少可以让你入门:
\documentclass[border=10mm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
\begin{filecontents}{data.csv}
100000000,0.0565325,-122.797,0.9984,-32.5345,0.9984,-32.5345,0.0565325,-122.271
101000000,0.0570362,-123.125,0.998372,-32.8592,0.998372,-32.8592,0.0570362,-122.593
102000000,0.0575381,-123.452,0.998343,-33.1838,0.998343,-33.1838,0.0575381,-122.915
103000000,0.0580382,-123.779,0.998314,-33.5085,0.998314,-33.5085,0.0580382,-123.237
\end{filecontents}
\pgfplotstableread[col sep=comma, header=false]{data.csv}\datatable
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={GHz},
ylabel={dB},
xtick=data,
x tick label style={
/pgf/number format/precision=3,
/pgf/number format/fixed,
/pgf/number format/fixed zerofill
}
]
\addplot table[
x expr={\thisrowno{0}/1e9},
y expr={20*log10(abs(\thisrowno{1}))}
] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}