如何在 pgfplots 中添加两个波形

如何在 pgfplots 中添加两个波形

在下面给出的示例代码中,我有两个图。我想添加第三个和第四个图,它们是原始两个图的总和和乘积。在 pgfplots 中有没有简单的方法可以做到这一点?

这是我的代码

%\documentclass[varwidth]{standalone}
\documentclass[tikz]{standalone}
\usepackage{varwidth}
\usepackage{pgfplots,pgfplotstable, booktabs}
\usepackage{bm}

\usepgfplotslibrary{groupplots}
\usepackage{filecontents}
\usepackage{graphicx}
\usepackage{float}

\begin{filecontents*}{MAG.dat}
  Iter   Abs1   Abs2      x      y
     1, 0.9317521, 1.1849326, 1.6130556, 0.8022207
     2, 1.8946202, 1.1228282, 1.8964566, -0.5353802
     3, 1.5243302, 1.0372991, 1.4375012, 0.9719003
     4, 1.5797030, 1.1346832, 1.8717142, 0.3138737
     5, 1.8814457, 1.0529187, 2.0568468, -0.5509391
     6, 2.0435003, 1.0470546, 2.0621956, -0.3565483
     7, 2.0373926, 1.1215579, 2.1836100, 0.3360301
     8, 1.9797077, 1.1632352, 1.8299063, 0.3871091
     9, 1.9972528, 1.1952478, 1.8133509, -0.0867033
    10, 1.8320176, 1.0625633, 1.0727495, 1.7256738
\end{filecontents*}

\pgfplotsset{compat=1.12}
\pgfplotsset{minor grid style={dotted,gray!90}}
\pgfplotsset{major grid style={gray!70!black},
            every tick label/.append style={font=\scriptsize},
            every axis plot/.append style={line width=0.8pt},
            }

\newcommand{\bod}{MAG.dat}

\begin{document}
\begin{figure}[H]
\centering
\begin{tikzpicture}
\begin{semilogxaxis}[width=14cm,height=10.2cm,
                    grid = both,
                    log ticks with fixed point,
                    every major grid/.style={gray, opacity=0.9},
                    title = {},
                    xlabel={\emph{\bfseries{Iteration}}},
                    ylabel={\emph{\bfseries{Temp}}},
                    no markers,
                    every axis plot/.append style={very thick},
                    legend style={at={(0,0)},anchor=south west},
                    legend cell align=left,
                    ]

\addplot+ [red]table[x index=0,y index=1, col sep=comma] {\bod};
\addlegendentry{$\zeta = 0.3$};
\addplot+ [blue]table[x index=0,y index=3, col sep=comma] {\bod};
\addlegendentry{$\zeta = 0.5$};

\addlegendentry{\emph{Sum}}

\end{semilogxaxis}
\end{tikzpicture}
\end{figure}
\end{document}

答案1

您可以使用,然后使用/y expr提及列\thisrow\thisrowno

\documentclass[]{standalone}
\usepackage{pgfplots}


\pgfplotstableread[col sep=comma]{
Iter,Abs1,Abs2,x,y
     1, 0.9317521, 1.1849326, 1.6130556, 0.8022207
     2, 1.8946202, 1.1228282, 1.8964566, -0.5353802
     3, 1.5243302, 1.0372991, 1.4375012, 0.9719003
     4, 1.5797030, 1.1346832, 1.8717142, 0.3138737
     5, 1.8814457, 1.0529187, 2.0568468, -0.5509391
     6, 2.0435003, 1.0470546, 2.0621956, -0.3565483
     7, 2.0373926, 1.1215579, 2.1836100, 0.3360301
     8, 1.9797077, 1.1632352, 1.8299063, 0.3871091
     9, 1.9972528, 1.1952478, 1.8133509, -0.0867033
    10, 1.8320176, 1.0625633, 1.0727495, 1.7256738
}\bod

\pgfplotsset{compat=1.12}
\pgfplotsset{minor grid style={dotted,gray!90}}
\pgfplotsset{major grid style={gray!70!black},
            every tick label/.append style={font=\scriptsize},
            every axis plot/.append style={line width=0.8pt},
            }
\begin{document}
\begin{tikzpicture}
\begin{semilogxaxis}[width=14cm,height=10.2cm,
                    grid = both,
                    log ticks with fixed point,
                    every major grid/.style={gray, opacity=0.9},
                    title = {},
                    xlabel={\emph{\bfseries{Iteration}}},
                    ylabel={\emph{\bfseries{Temp}}},
                    no markers,
                    every axis plot/.append style={very thick},
                    legend style={at={(0,0)},anchor=south west},
                    legend cell align=left,
                    ]

\addplot+ [red]table[x index=0,y index=1 , col sep=comma] {\bod};
\addlegendentry{$\zeta = 0.3$};
\addplot+ [blue]table[x index=0,y index=3, col sep=comma] {\bod};
\addlegendentry{$\zeta = 0.5$};
\addplot+ [gray,ultra thick]table[x index=0,y expr=\thisrowno{1}+\thisrowno{3}, col sep=comma] {\bod};
\addlegendentry{\emph{Sum}}
\addplot+ [orange,ultra thick]table[x index=0,y expr=\thisrowno{1}*\thisrowno{3}, col sep=comma] {\bod};
\addlegendentry{\emph{Product}}

\end{semilogxaxis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容