![pgfplots 宏:x filter/.code={\pgfmathparse{#1/1000000}\pgfmathresult} 不适用于 csv 文件](https://linux22.com/image/480232/pgfplots%20%E5%AE%8F%EF%BC%9Ax%20filter%2F.code%3D%7B%5Cpgfmathparse%7B%231%2F1000000%7D%5Cpgfmathresult%7D%20%E4%B8%8D%E9%80%82%E7%94%A8%E4%BA%8E%20csv%20%E6%96%87%E4%BB%B6.png)
我想要 x 轴在 MHz 范围内,因此我想要使用命令:
x filter/.code={\pgfmathparse{#1/1000000}\pgfmathresult}
但什么都没发生。我正在使用 lualatex 进行图像编译。CSV 文件是从实验室仪器获得的,我不想每次生成新文件时都更改它。我尝试按照 4.22 节完成这项工作pgfplots 手册(跳过或更改坐标 - 过滤器)。
我的 MNWE 是:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgflibrary{luamath}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\begin{semilogyaxis}[
% title={ Quartz Crystals Resonance Frequencies },
xlabel={ Frequency [\si{\MHz}] },
ylabel={ Impedance [\si{\ohm}] },
xmajorgrids=true,
ymajorgrids=true,
yminorgrids=true,
grid style=solid,
tick scale binop=\times, % page 347 man. ver. 1.15
cycle list name = color list, % page 212 man. ver. 1.15
]
\addplot
table[
x index=0, y index=3,
/pgf/number format/read comma as period,
skip first n=31,
x filter/.code={\pgfmathparse{#1/1000000}\pgfmathresult},
col sep=semicolon,
] {tikz003_data.csv};
\legend{Impedance}
\end{semilogyaxis}
\end{tikzpicture}
\end{document}
CSV文件的相关内容如下:
Frequency (Hz);Trace 1: Impedance: Real (Ω);Trace 1: Impedance: Imaginary (Ω);Trace 1: Impedance: Magnitude (Ω)
11000000;37,52887681;-5113,935907;5114,073609
11000977,04;37,60450902;-5115,102842;5115,241068
11001954,08;37,43878572;-5114,644072;5114,781094
11002931,12;36,8805983;-5113,873122;5114,006109
11003908,16;37,27322288;-5112,017215;5112,153099
11004885,2;36,9557493;-5111,327511;5111,461107
11005862,24;38,75948108;-5111,658528;5111,805474
11006839,28;37,43056578;-5112,087105;5112,224136
11007816,32;39,73565309;-5110,324112;5110,478593
11008793,36;37,00349591;-5110,290774;5110,424743
11009770,4;38,17700954;-5111,313222;5111,455795
11010747,44;36,67828736;-5109,138839;5109,270493
11011724,47;35,88358107;-5104,672947;5104,799068
答案1
这是(原则上)一个使用表数据的简单任务。有关详细信息,请查看代码中的注释。
% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{semilogyaxis}[
xlabel={Frequency in \si{\MHz}},
ylabel={Impedance in \si{\ohm}},
xmajorgrids=true,
ymajorgrids=true,
yminorgrids=true,
grid style=solid,
cycle list name=color list,
]
\addplot table[
% % replace this ...
% x index=0,
% ... with this where you can manipulate table data "as you like"
x expr={\thisrowno{0}/1e6},
y index=3,
% % because with commas as decimal separators an unexpected error
% % occurred I replaced all commas with periods which made it work
% % (this seems to be a bug)
% /pgf/number format/read comma as period,
col sep=semicolon,
] {
Frequency (Hz);Trace 1: Impedance: Real (Ω);Trace 1: Impedance: Imaginary (Ω);Trace 1: Impedance: Magnitude (Ω)
11000000;37.52887681;-5113.935907;5114.073609
12000000.05;37.60450902;-5115.102842;18.5
13000000;35.88358107;-5104.672947;380000
};
\legend{Impedance}
\end{semilogyaxis}
\end{tikzpicture}
\end{document}