![对 PGF/Tikz 图内的加载数据应用数学运算](https://linux22.com/image/339640/%E5%AF%B9%20PGF%2FTikz%20%E5%9B%BE%E5%86%85%E7%9A%84%E5%8A%A0%E8%BD%BD%E6%95%B0%E6%8D%AE%E5%BA%94%E7%94%A8%E6%95%B0%E5%AD%A6%E8%BF%90%E7%AE%97.png)
我想要的是: \abs{S21/S12-1}*100%
其中 S21 和 S12 是 txt 文件中的列
我拥有的: 真的不知道。我非常不确定在 PGF/tikz 中是否有可能进行这种计算。如果不能,TeX 中有什么替代方案?PGF数学?
(我仍然可以把所有的东西带到 Matlab,计算,然后在 TeX 中重新导入,但是......嗯。)
我尝试过 我阅读了以下三篇文章并尝试进行调整:1,2和3- 后者给了我这样的想法,即基本操作应该是可行的,但由于解析器的存在,实现起来有些棘手。
下面是我想要获得的 MWE(乐观地称之为;它不能编译)。
\documentclass{article}
\usepackage{amsmath,amsthm}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14}
% sample values, extracted from 100/dB_IC_10mA_S21.txt and 100/dB_IC_10mA_S12.txt
\pgfplotstableread[col sep=space]{%
f1 s21
3.00000000000000000E5 -5.37923824569095640E-1
9.24624999999999990E5 -5.23021516857375080E-1
1.54925000000000020E6 -5.24032299826929030E-1
2.17387499999999980E6 -5.18575016774617570E-1
2.79850000000000020E6 -5.17614614317642550E-1
3.42312500000000020E6 -5.28421012169730810E-1
4.04774999999999970E6 -5.25424891213584110E-1
4.67237500000000060E6 -5.24097415339730870E-1
5.29700000000000060E6 -5.28626243766300340E-1
5.92162500000000060E6 -5.38603437681817350E-1
6.54625000000000060E6 -5.24872720444434120E-1
7.17087499999999970E6 -5.26809061504114420E-1
7.79549999999999970E6 -5.21513983574569910E-1
8.42012500000000050E6 -5.35052438394172110E-1
}\datas
\pgfplotstableread[col sep=space]{%
f2 s12
3.00000000000000000E5 -4.61253023410550080E-1
9.24624999999999990E5 -4.44818670150491350E-1
1.54925000000000020E6 -4.45906534066983800E-1
2.17387499999999980E6 -4.54506559283727720E-1
2.79850000000000020E6 -4.50208906640044760E-1
3.42312500000000020E6 -4.52753227856168470E-1
4.04774999999999970E6 -4.53657966529585540E-1
4.67237500000000060E6 -4.64282209437626040E-1
5.29700000000000060E6 -4.51387968956751350E-1
5.92162500000000060E6 -4.59361196203085690E-1
6.54625000000000060E6 -4.66558553131420340E-1
7.17087499999999970E6 -4.44614963756639090E-1
7.79549999999999970E6 -4.57214959955166740E-1
8.42012500000000050E6 -4.54436434169834770E-1
}\datat
\begin{document}
\begin{tikzpicture}
\begin{axis}[
no markers,%
]
% \addplot+[thin] table [col sep=space]{100/dB_IC_10mA_S12.txt};
% complete file of measurement data
% \addplot+[thin] table [col sep=space]{100/dB_IC_10mA_S21.txt};
% complete file of measurement data
% \addplot+[thin] table [col sep=space]\abs{{100/dB_IC_10mA_S12.txt}-{100/dB_IC_10mA_S21.txt}};
% not working
% \addplot+[thin] table [col sep=space]\abs{\datas - \datat};
% not working
\addplot+[thin] table [col sep=space] (${\abs{\datas - \datat}}$);
\addplot+[thin] table[col sep=space, x=(${\abs{\datas - \datat}}$),y=f2]
\end{axis}
\end{tikzpicture}
\end{document}
答案1
首先,我认为您需要合并两个表,以便能够在同一图表中使用两个表的数据。例如,参见将两个表合并为一个一种方法。基本上,你可以使用以下命令将一个表中的列插入另一个表中
\pgfplotstablecreatecol[
copy column from table={\datas}{[index] 1},
]{s21}{\datat}
要绘制基于表中列的计算结果,可以使用y expr=<calculation>
,并且在计算中可以使用\thisrow{<column name>}
来访问表中的值。所以如果我理解正确的话,你想要y expr={abs(\thisrow{s21}/\thisrow{s12}-1)*100}
,它用作
\addplot+[thin] table[x=f1,y expr={abs(\thisrow{s21}/\thisrow{s12}-1)*100}] {\datat};
完整代码:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.13}
% sample values, extracted from 100/dB_IC_10mA_S21.txt and 100/dB_IC_10mA_S12.txt
\pgfplotstableread[col sep=space]{%
f1 s21
3.00000000000000000E5 -5.37923824569095640E-1
9.24624999999999990E5 -5.23021516857375080E-1
1.54925000000000020E6 -5.24032299826929030E-1
2.17387499999999980E6 -5.18575016774617570E-1
2.79850000000000020E6 -5.17614614317642550E-1
3.42312500000000020E6 -5.28421012169730810E-1
4.04774999999999970E6 -5.25424891213584110E-1
4.67237500000000060E6 -5.24097415339730870E-1
5.29700000000000060E6 -5.28626243766300340E-1
5.92162500000000060E6 -5.38603437681817350E-1
6.54625000000000060E6 -5.24872720444434120E-1
7.17087499999999970E6 -5.26809061504114420E-1
7.79549999999999970E6 -5.21513983574569910E-1
8.42012500000000050E6 -5.35052438394172110E-1
}\datas
\pgfplotstableread[col sep=space]{%
f2 s12
3.00000000000000000E5 -4.61253023410550080E-1
9.24624999999999990E5 -4.44818670150491350E-1
1.54925000000000020E6 -4.45906534066983800E-1
2.17387499999999980E6 -4.54506559283727720E-1
2.79850000000000020E6 -4.50208906640044760E-1
3.42312500000000020E6 -4.52753227856168470E-1
4.04774999999999970E6 -4.53657966529585540E-1
4.67237500000000060E6 -4.64282209437626040E-1
5.29700000000000060E6 -4.51387968956751350E-1
5.92162500000000060E6 -4.59361196203085690E-1
6.54625000000000060E6 -4.66558553131420340E-1
7.17087499999999970E6 -4.44614963756639090E-1
7.79549999999999970E6 -4.57214959955166740E-1
8.42012500000000050E6 -4.54436434169834770E-1
}\datat
\pgfplotstablecreatecol[
copy column from table={\datas}{[index] 0},
]{f1}{\datat}
\pgfplotstablecreatecol[
copy column from table={\datas}{[index] 1},
]{s21}{\datat}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
no markers,%
]
\addplot+[thin] table[x=f1,y=s21] {\datat};
\addplot+[thin] table[x=f2,y=s12] {\datat};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
no markers,%
]
\addplot+[thin] table[x=f1,y expr={abs(\thisrow{s21}/\thisrow{s12}-1)*100}] {\datat};
\end{axis}
\end{tikzpicture}
\end{document}