对 PGF/Tikz 图内每列的加载数据应用数学运算

对 PGF/Tikz 图内每列的加载数据应用数学运算

我是 LaTeX 绘图系统的新手,我被一个问题搞晕了,我有一个 csv 文件,它有 11 列,我想绘制第 2 到第 11 列与第 1 列的对比图。所以我按照以下代码进行操作(取自 stackexchange,谢谢)。我的问题如下,我想用 x 列除以 x 列中的最大值,再用每个 y 列除以所有 y 列中的最大值,

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=1.0\textwidth,
scale only axis,
xlabel={$x$},
ylabel={Column Data}]

% Graph column 0 versus column 1
\addplot table[x index=0 ,y index=1,col sep=comma] {Axial.csv};
\addlegendentry{200}% y index+1 since humans count from 1

% Graph column 0 versus column 2    
\addplot table[x index=0,y index=2,col sep=comma] {Axial.csv};
\addlegendentry{500}

% Graph column 0 versus column 3    
\addplot table[x index=0,y index=3,col sep=comma] {Axial.csv};
\addlegendentry{1000}

% Graph column 0 versus column 4    
\addplot table[x index=0,y index=4,col sep=comma] {Axial.csv};
\addlegendentry{2000}

% Graph column 0 versus column 5    
\addplot table[x index=0,y index=5,col sep=comma] {Axial.csv};
\addlegendentry{3000}

% Graph column 0 versus column 6    
\addplot table[x index=0,y index=6,col sep=comma] {Axial.csv};
\addlegendentry{4000}

% Graph column 0 versus column 7    
\addplot table[x index=0,y index=7,col sep=comma] {Axial.csv};
\addlegendentry{5000}

% Graph column 0 versus column 8    
\addplot table[x index=0,y index=8,col sep=comma] {Axial.csv};
\addlegendentry{6000}

% Graph column 0 versus column 9    
\addplot table[x index=0,y index=9,col sep=comma] {Axial.csv};
\addlegendentry{7000}

% Graph column 0 versus column 10    
\addplot table[x index=0,y index=10,col sep=comma] {Axial.csv};
\addlegendentry{8000}

\end{axis}
\end{tikzpicture}
\end{document}

如果你能帮助我解决这个问题,我将不胜感激。最好的

答案1

好的,我们开始吧。我创建了一个宏,用于查找列的最小值和最大值。起点是这个答案但我对其进行了一些修改。如果这个函数已经在某个地方内置,我一点也不会感到惊讶,至少在内部它一定是内置的,因为它的point meta工作方式。然后我按照要求,通过除以最大值来创建由原始列组成的新列。为此,我使用了复制和粘贴,因为这比在循环中天真地尝试这样做时遇到的扩展问题更快。我更改了代码,使得 csv 文件只读取一次。这是代码。

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\newcommand{\findminmax}[1]{% https://tex.stackexchange.com/a/107364/121799
  % Count rows
  \pgfplotstablegetrowsof{\mytable}
  \pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}
  \typeout{\numrows\space rows}
  % Initiate max value
  \pgfplotstablegetelem{0}{#1}\of{\mytable}
  \pgfmathtruncatemacro{\mymax}{\pgfplotsretval}
  \pgfmathtruncatemacro{\mymin}{\pgfplotsretval}
  \typeout{initially:\space\mymin}
  \pgfplotsinvokeforeach {1,...,\numrows}{
    \pgfplotstablegetelem{##1}{#1}\of{\mytable}
    \pgfmathsetmacro{\mymax}{max(\pgfplotsretval,\mymax)}
    \pgfmathsetmacro{\mymin}{min(\pgfplotsretval,\mymin)}
   }
  \let\ymax=\mymax%
  \let\ymin=\mymin%
}



\begin{document}
\pgfplotstableread[col sep=comma,header=true]{%
Axial.csv}\mytable

\findminmax{0}
\let\xmax=\ymax
\pgfplotstablecreatecol[expr={(\thisrow{0})/\xmax}]{newx}{\mytable}
% \pgfplotsinvokeforeach{1,2,...,10}{\findminmax{#1}
% \typeout{#1:\ymin-\ymax}
% \pgfplotstablecreatecol[expr={(\thisrow{#1})/\ymax}]{new#1}{\mytable}
% }
%
% yes, the following is very ugly, but faster than fumbling with the expansion
% magic that comes with pgfplots(table), at least for non-wizards like me ;-)
\findminmax{1}
\pgfplotstablecreatecol[expr={(\thisrow{1})/\ymax}]{new1}{\mytable}
\findminmax{2}
\pgfplotstablecreatecol[expr={(\thisrow{1})/\ymax}]{new2}{\mytable}
\findminmax{3}
\pgfplotstablecreatecol[expr={(\thisrow{1})/\ymax}]{new3}{\mytable}
\findminmax{4}
\pgfplotstablecreatecol[expr={(\thisrow{1})/\ymax}]{new4}{\mytable}
\findminmax{5}
\pgfplotstablecreatecol[expr={(\thisrow{1})/\ymax}]{new5}{\mytable}
\findminmax{6}
\pgfplotstablecreatecol[expr={(\thisrow{1})/\ymax}]{new6}{\mytable}
\findminmax{7}
\pgfplotstablecreatecol[expr={(\thisrow{1})/\ymax}]{new7}{\mytable}
\findminmax{8}
\pgfplotstablecreatecol[expr={(\thisrow{1})/\ymax}]{new8}{\mytable}
\findminmax{9}
\pgfplotstablecreatecol[expr={(\thisrow{1})/\ymax}]{new9}{\mytable}
\findminmax{10}
\pgfplotstablecreatecol[expr={(\thisrow{1})/\ymax}]{new10}{\mytable}


\begin{tikzpicture}
\begin{axis}[
width=1.0\textwidth,
scale only axis,
xlabel={$x$},
ylabel={Column Data}]


% Graph column 0 versus column 1
\addplot table[x=newx ,y=new1,col sep=comma] \mytable;
\addlegendentry{200}% y index+1 since humans count from 1

% Graph column 0 versus column 2    
\addplot table[x=newx,y=new2,col sep=comma] \mytable;
\addlegendentry{500}

% Graph column 0 versus column 3    
\addplot table[x=newx,y=new3,col sep=comma] \mytable;
\addlegendentry{1000}

% Graph column 0 versus column 4    
\addplot table[x=newx,y=new4,col sep=comma] \mytable;
\addlegendentry{2000}

% Graph column 0 versus column 5    
\addplot table[x=newx,y=new5,col sep=comma] \mytable;
\addlegendentry{3000}

% Graph column 0 versus column 6    
\addplot table[x=newx,y=new6,col sep=comma] \mytable;
\addlegendentry{4000}

% Graph column 0 versus column 7    
\addplot table[x=newx,y=new7,col sep=comma] \mytable;
\addlegendentry{5000}

% Graph column 0 versus column 8    
\addplot table[x=newx,y=new8,col sep=comma] \mytable;
\addlegendentry{6000}

% Graph column 0 versus column 9    
\addplot table[x=newx,y=new9,col sep=comma] \mytable;
\addlegendentry{7000}

% Graph column 0 versus column 10    
\addplot table[x=newx,y=new10,col sep=comma] \mytable;
\addlegendentry{8000}

\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容