pgfplotstableread + addplot 表:缩放列以供重复使用

pgfplotstableread + addplot 表:缩放列以供重复使用

我从代码中获取输出文件,并希望使用pgfplots。我想用相同的文件创建几个不同的图。因此,我用宏读取它们pgfplotstable

我知道第一列包含以秒为单位的 x 轴值。对于每个图,我想将 x 轴单位更改为毫秒。目前,我通过将 x 轴值乘以一千来实现这一点。addplotx expr=\thisrowno{0}*1000我看来,这浪费了内存和编译时间。

是否可以缩放已经存在的第一列值pgfplotstableread

在本主题中我找到了columns/0/.style={preproc/expr={##1*1000}}for pgfplotstabletypeset,但找不到类似的命令来永久修改里面的值\chartdata。我也看了一下write to macrofor pgfplotstableset,但无法让它工作。

\documentclass{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{pgfplotstable}

\begin{document}

\begin{filecontents}{chartdata.txt}
time               value_1            value_2            value_3
s                  some_unit          some_unit          some_unit
0.                 0.                 0.                 0.         
100.E-09            27.541E-06        580.194E-09         19.8912E-06 
200.E-09            55.082E-06          1.18929E-06       40.2063E-06 
350.E-09            96.3935E-06         2.15546E-06       70.5431E-06 
575.E-09           158.361E-06          3.67572E-06      116.044E-06  
87.4788E-06        24.0925E-03       634.302E-06         17.6932E-03 
652.755E-06        179.775E-03          4.74164E-03      131.941E-03  
750.568E-06        206.714E-03         12.7344E-03       152.076E-03  
760.034E-06        209.321E-03         20.4248E-03       154.317E-03  
760.941E-06        209.571E-03         20.4985E-03       154.679E-03  
\end{filecontents}

\pgfplotstableread[
  skip first n=6, % 2+4 for filecontents header
]{chartdata.txt}{\chartdata}

\begin{tikzpicture}

  \begin{axis}[
    cycle list name=color list,
  ]
    \addplot+[
      % currenty no options
    ] table [
      x expr=\thisrowno{0}*1000,
      y expr=\thisrowno{1},
    ]{\chartdata};
    \addlegendentry{Value 1}
    \addplot+[
      % currenty no options
    ] table [
      x expr=\thisrowno{0}*1000,
      y expr=\thisrowno{2},
    ]{\chartdata};
    \addlegendentry{Value 2}
    \addplot+[
      % currenty no options
    ] table [
      x expr=\thisrowno{0}*1000,
      y expr=\thisrowno{3},
    ]{\chartdata};
    \addlegendentry{Value 3}
  \end{axis}
\end{tikzpicture}

\end{document}

答案1

你可以创建新列

\pgfplotstablecreatecol
  [create col/expr={\thisrowno{0}*1000}]
  {time in ms}% column name
  \chartdata

x=time in ms并在图中使用它。

\documentclass{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{pgfplotstable}

\begin{document}

\begin{filecontents}{chartdata.txt}
time               value_1            value_2            value_3
s                  some_unit          some_unit          some_unit
0.                 0.                 0.                 0.         
100.E-09            27.541E-06        580.194E-09         19.8912E-06 
200.E-09            55.082E-06          1.18929E-06       40.2063E-06 
350.E-09            96.3935E-06         2.15546E-06       70.5431E-06 
575.E-09           158.361E-06          3.67572E-06      116.044E-06  
87.4788E-06        24.0925E-03       634.302E-06         17.6932E-03 
652.755E-06        179.775E-03          4.74164E-03      131.941E-03  
750.568E-06        206.714E-03         12.7344E-03       152.076E-03  
760.034E-06        209.321E-03         20.4248E-03       154.317E-03  
760.941E-06        209.571E-03         20.4985E-03       154.679E-03  
\end{filecontents}

\pgfplotstableread[
  skip first n=6, % 2+4 for filecontents header
]{chartdata.txt}{\chartdata}

\pgfplotstablecreatecol
  [create col/expr={\thisrowno{0}*1000}]
  {time in ms}% column name
  \chartdata

\begin{tikzpicture}
  \begin{axis}[
    cycle list name=color list,
  ]
    \addplot+[
      % currenty no options
    ] table [
      x=time in ms,
      y index=1,
    ]{\chartdata};
    \addlegendentry{Value 1}
    \addplot+[
      % currenty no options
    ] table [
      x=time in ms,
      y index=2,
    ]{\chartdata};
    \addlegendentry{Value 2}
    \addplot+[
      % currenty no options
    ] table [
      x=time in ms,
      y index=3,
    ]{\chartdata};
    \addlegendentry{Value 3}
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述


另一种可能性是修改第一列

\documentclass{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{pgfplotstable}

\begin{document}

\begin{filecontents}{chartdata.txt}
time               value_1            value_2            value_3
s                  some_unit          some_unit          some_unit
0.                 0.                 0.                 0.         
100.E-09            27.541E-06        580.194E-09         19.8912E-06 
200.E-09            55.082E-06          1.18929E-06       40.2063E-06 
350.E-09            96.3935E-06         2.15546E-06       70.5431E-06 
575.E-09           158.361E-06          3.67572E-06      116.044E-06  
87.4788E-06        24.0925E-03       634.302E-06         17.6932E-03 
652.755E-06        179.775E-03          4.74164E-03      131.941E-03  
750.568E-06        206.714E-03         12.7344E-03       152.076E-03  
760.034E-06        209.321E-03         20.4248E-03       154.317E-03  
760.941E-06        209.571E-03         20.4985E-03       154.679E-03  
\end{filecontents}

\pgfplotstableread[
  skip first n=6, % 2+4 for filecontents header
]{chartdata.txt}{\chartdata}


\pgfplotstablemodifyeachcolumnelement{[index]0}\of\chartdata\as\cell{%
  \pgfkeys{/pgf/fpu=true}%
  \pgfmathparse{1000*\cell}%
  \pgfkeys{/pgf/fpu=false}%
  \edef\cell{\pgfmathresult}%
}%

\begin{tikzpicture}
  \begin{axis}[
    cycle list name=color list,
  ]
    \addplot+[
      % currenty no options
    ] table [
      x index=0,
      y index=1,
    ]{\chartdata};
    \addlegendentry{Value 1}
    \addplot+[
      % currenty no options
    ] table [
      x index=0,
      y index=2,
    ]{\chartdata};
    \addlegendentry{Value 2}
    \addplot+[
      % currenty no options
    ] table [
      x index=0,
      y index=3,
    ]{\chartdata};
    \addlegendentry{Value 3}
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述


第三种可能性是仅使用 xtick 标签进行缩放

scaled x ticks=false,
xticklabel={%
  \pgfmathparse{\tick*1000}%
  $\pgfmathprintnumber[fixed,precision=1]{\pgfmathresult}$%
}

作为axis环境的选择。

\documentclass{standalone}
\usepackage{filecontents}
\usepackage{xintexpr}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{pgfplotstable}

\begin{document}

\begin{filecontents}{chartdata.txt}
time               value_1            value_2            value_3
s                  some_unit          some_unit          some_unit
0.                 0.                 0.                 0.         
100.E-09            27.541E-06        580.194E-09         19.8912E-06 
200.E-09            55.082E-06          1.18929E-06       40.2063E-06 
350.E-09            96.3935E-06         2.15546E-06       70.5431E-06 
575.E-09           158.361E-06          3.67572E-06      116.044E-06  
87.4788E-06        24.0925E-03       634.302E-06         17.6932E-03 
652.755E-06        179.775E-03          4.74164E-03      131.941E-03  
750.568E-06        206.714E-03         12.7344E-03       152.076E-03  
760.034E-06        209.321E-03         20.4248E-03       154.317E-03  
760.941E-06        209.571E-03         20.4985E-03       154.679E-03  
\end{filecontents}

\pgfplotstableread[
  skip first n=6, % 2+4 for filecontents header
]{chartdata.txt}{\chartdata}

\begin{tikzpicture}
  \begin{axis}[
    cycle list name=color list,
    scaled x ticks=false,
    xticklabel={%
      \pgfmathparse{\tick*1000}%
      $\pgfmathprintnumber[fixed,precision=1]{\pgfmathresult}$%
    }
  ]
    \addplot+[
      % currenty no options
    ] table [
      x index=0,
      y index=1,
    ]{\chartdata};
    \addlegendentry{Value 1}
    \addplot+[
      % currenty no options
    ] table [
      x index=0,
      y index=2,
    ]{\chartdata};
    \addlegendentry{Value 2}
    \addplot+[
      % currenty no options
    ] table [
      x index=0,
      y index=3,
    ]{\chartdata};
    \addlegendentry{Value 3}
  \end{axis}
\end{tikzpicture}
\end{document}

由 krtek 编辑:

对于大于 >16383.9998 的值,例如 y 轴使用fpu引擎:

scaled y ticks=false,
yticklabel={%
  \pgfkeys{/pgf/fpu=true}%
  \pgfmathparse{\tick*1000}%
  \pgfkeys{/pgf/fpu=false}
  $\pgfmathprintnumber[
    fixed,
    precision=1,
  ]{\pgfmathresult}$%
}

相关内容