访问 gnuplot 变量

访问 gnuplot 变量

是否可以从 LaTeX 访问 gnuplot 变量而无需写入外部文件?

我知道的唯一方法是从 gnuplot 写入文件,然后从 LaTeX 再次读取。但是,这很麻烦,而且需要您跟踪文件名和变量的顺序。有没有更好的方法?

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}

\begin{filecontents*}{data.csv}
  X Y
  1.000 0.00
  2.000 2.00
  3.000 6
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
  \begin{axis}
    \addplot[only marks] table {data.csv};
    \addlegendentry{Data}

    \addplot+[raw gnuplot, no markers] gnuplot {
      stats 'data.csv'; % Calculate a bunch of statistical values
      plot [x=0:5] STATS_slope*x + STATS_intercept; % plot the linear fit
      set print "parameters.dat"; % Open a file to save the parameters into
      print STATS_slope; % Write the parameters to file
      print STATS_intercept;
      print STATS_correlation;x
    };

    \newread\myread % Open the file again
    \openin\myread=parameters.dat
    \read\myread to \mytmp \pgfmathprintnumberto{\mytmp}{\slope} % Read the values from the file and pprint them to macros
    \read\myread to \mytmp \pgfmathprintnumberto{\mytmp}{\intercept}
    \read\myread to \mytmp \pgfmathpow{\mytmp}{2} \pgfmathprintnumberto[precision=5]{\pgfmathresult}{\Rsq} % Square the r value before pprinting
    \addlegendentry{$y=\slope \cdot x + \intercept$, ${R}^{2}=\Rsq$}

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

答案1

我最终编写了一个跟踪文件的命令。它仍然将变量写入外部文件,但由于您不需要跟踪文件名,因此使用起来更加容易。我希望这可以帮助其他人将快速线性回归添加到图中。

\documentclass{article}
\usepackage{pgfplots,filecontents}
\pgfplotsset{compat=1.14}

\newcommand{\linregAddplot}[2][]{%
  % Will plot a linear regression from a datafile.
  %
  % Usage: \linregAddplot[<options>]{<file>}
  % where <options> is the same options as for \addplot+ and <file> is a simple
  % datafile gnuplot can read.
  %
  % The macros \linregSlope, \linregIntercept and \linregRsq are available after
  % \linregAddplot has been called. They can be used in
  % \addlegendentry{}.

    \addplot+[raw gnuplot, no markers, #1] gnuplot {
    stats '#2' nooutput; % Calculate a bunch of statistical values
    plot [x=STATS_min_x:STATS_max_x] STATS_slope*x + STATS_intercept;
    set print "#2.linreg";
    print STATS_slope; % Write the parameters to file
    print STATS_intercept;
    print STATS_correlation;
  };
  \IfFileExists{./#2.linreg}{
    \expandafter\newread\csname read#2\endcsname % Open the file again
    \expandafter\openin\csname read#2\endcsname=#2.linreg

    % Read the values from the file
    \expandafter\read\csname read#2\endcsname to \mytmp
    % and pprint them to tmp macros
    \pgfmathprintnumberto{\mytmp}{\linregSlope}

    \expandafter\read\csname read#2\endcsname to \mytmp
    \pgfmathprintnumberto{\mytmp}{\linregIntercept}

    \expandafter\read\csname read#2\endcsname to \mytmp
    % Square the r value before pprinting
    \pgfmathpow{\mytmp}{2}
    \pgfmathprintnumberto[precision=5]{\pgfmathresult}{\linregRsq}
      }{%
    \def\linregSlope{NaN}
    \def\linregIntercept{NaN}
    \def\linregRsq{NaN}
    \message{You need to run LaTeX again to get you linear regression labels right.}
  }
}

\begin{document}

\begin{filecontents*}{data.csv}
  X Y
  1.000 0.00
  -2.000 2.00
  3.000 14
\end{filecontents*}

\begin{filecontents*}{data2.csv}
  X Y
  -1.000 0.00
  -2.000 2.00
  3.000 -3
\end{filecontents*}

\begin{tikzpicture}
  \begin{axis}
    \addplot[only marks] table {data.csv};
    \addlegendentry{Data}

    \linregAddplot{data.csv}
    \addlegendentry{$y=\linregSlope \cdot x + \linregIntercept$, ${R}^{2}=\linregRsq$}

    \addplot[only marks] table {data2.csv};
    \addlegendentry{Data2}

    \linregAddplot[dashed]{data2.csv}
    \addlegendentry{$y=\linregSlope \cdot x + \linregIntercept$, ${R}^{2}=\linregRsq$}

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

相关内容