pgfplots:如何使用从文件中获取的数字定义变量?

pgfplots:如何使用从文件中获取的数字定义变量?

在研究科学课题时,我想知道以下情况是否可行:我想使用命令绘制几条指数曲线\addplot。特殊之处在于我需要的指数不是硬编码的,而是从文本文件中读取的!这怎么能实现呢?我创建了一个 MWE 来阐明我想要什么:

%% MWE for SX: Defining variables with numbers taken from a file
\documentclass{standalone}

\usepackage[utf8]{inputenx}
\usepackage[T1]{fontenc}
%% Use only sans-serif fonts; change to serif if desired
\renewcommand*\sfdefault{phv}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{arevmath}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}
\usetikzlibrary{spy, backgrounds}
\usepackage{pgfplotstable}
\usepackage{xcolor}
\usepackage{amsmath}
%% To read variables from file
\usepackage{datatool}
\usepackage{filecontents}

\newlength\figurewidth
\newlength\figureheight
\newlength\marksize

\begin{document}
%% Width and height of the output figure, adapt as necessary
\setlength{\figurewidth}{13cm}
\setlength{\figureheight}{8cm}
\setlength{\marksize}{2.4pt}
\setlength{\linewidth}{1pt}

%% Define the file in here, as whole files cannot be uploaded to Tex.SE
\begin{filecontents}{LsqExponents.csv}
4.012,3.456,2.345
\end{filecontents}
%% Now read these three values from the file
\DTLloaddb[noheader, keys={b_annu, b_rest, b_stra}]{LsqExponents}{LsqExponents.csv}
%\DTLdisplaydb{LsqExponents}

\begin{tikzpicture}[font=\large]

\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=0.05,
xmax=0.5,
xtick={0.1,0.2,0.3,0.4,0.5},
xlabel={$\text{T G S } \gamma_\mathrm{g} \text{ (-)}$},
%xmajorgrids,
ymin=0,
ymax=0.12,
ylabel={$\text{G R P } h_\mathrm{g} \text{ (-)}$},
yticklabel style={/pgf/number format/fixed,
/pgf/number format/precision=2,
/pgf/number format/fixed zerofill},
%ymajorgrids,
name=plot1,
legend pos=north west,
legend style={anchor=north west,draw=black,fill=white,legend cell align=left, rounded corners=2pt, nodes={inner sep=4pt,text depth=0pt}}
]
%% Fits and functions
\addplot [color=cyan, solid, domain=0:0.5] {x^4.567};
\addlegendentry{Original TD Fit};

\addplot [color=magenta, dashed, domain=0:0.5] {x^3.456};
\addlegendentry{TD-like Fit for Rest};

\addplot [color=black, dashed, domain=0:0.5] {x^2.345};
\addlegendentry{TD-like Fit for Stra};

\end{axis}

\end{tikzpicture}

\end{document}

如您所见,指数现在是硬编码的(第 65-71 行),但应该是浮点变量,取自 csv 文件(由另一个程序预先生成)!我对这个伟大的datatool包,但我不确定如何使用它,甚至不知道是否可以使用它来满足我的需求。该\DTLdisplaydb{LsqExponents}命令失败,这就是为什么它在 MWE 中被注释掉。

所以我猜解决方案包括两个步骤:

  1. 在 LaTeX/Tikz 中将三个数字作为单独的变量。
  2. 用这些变量替换硬编码的指数。

任何帮助都将不胜感激!

答案1

这里我使用我的包进行了实现readarray,它读取空格分隔的数据。关键行是

\begin{filecontents*}{LsqExponents.ssv}
4.012 3.456 2.345
\end{filecontents*}
%% Now read these three values from the file
\readdef{LsqExponents.ssv}{\mydatadef}
\readArrayij{\mydatadef}{mydata}{1}

它的作用是将文件内容读入\def名为 的\mydatadef。然后,我从中读取\mydatadef并将其排序为名为 的二维数组结构,mydata其列宽 = 1(因此实际上是一维数组)。使用以下方式访问数据值\arrayij{mydata}{<row value>}{1}

%% MWE for SX: Defining variables with numbers taken from a file
\documentclass{standalone}

\usepackage[utf8]{inputenx}
\usepackage[T1]{fontenc}
%% Use only sans-serif fonts; change to serif if desired
\renewcommand*\sfdefault{phv}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{arevmath}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}
\usetikzlibrary{spy, backgrounds}
\usepackage{pgfplotstable}
\usepackage{xcolor}
\usepackage{amsmath}
%% To read variables from file
\usepackage{readarray}
\usepackage{filecontents}

\newlength\figurewidth
\newlength\figureheight
\newlength\marksize

\begin{document}
%% Width and height of the output figure, adapt as necessary
\setlength{\figurewidth}{13cm}
\setlength{\figureheight}{8cm}
\setlength{\marksize}{2.4pt}
\setlength{\linewidth}{1pt}

%% Define the file in here, as whole files cannot be uploaded to Tex.SE
\begin{filecontents*}{LsqExponents.ssv}
4.012 3.456 2.345
\end{filecontents*}
%% Now read these three values from the file
\readdef{LsqExponents.ssv}{\mydatadef}
\readArrayij{\mydatadef}{mydata}{1}

\begin{tikzpicture}[font=\large]

\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=0.05,
xmax=0.5,
xtick={0.1,0.2,0.3,0.4,0.5},
xlabel={$\text{T G S } \gamma_\mathrm{g} \text{ (-)}$},
%xmajorgrids,
ymin=0,
ymax=0.12,
ylabel={$\text{G R P } h_\mathrm{g} \text{ (-)}$},
yticklabel style={/pgf/number format/fixed,
/pgf/number format/precision=2,
/pgf/number format/fixed zerofill},
%ymajorgrids,
name=plot1,
legend pos=north west,
legend style={anchor=north west,draw=black,fill=white,legend cell align=left, rounded corners=2pt, nodes={inner sep=4pt,text depth=0pt}}
]
%% Fits and functions
\addplot [color=cyan, solid, domain=0:0.5] {x^\arrayij{mydata}{1}{1}};
\addlegendentry{Original TD Fit};

\addplot [color=magenta, dashed, domain=0:0.5] {x^\arrayij{mydata}{2}{1}};
\addlegendentry{TD-like Fit for Rest};

\addplot [color=black, dashed, domain=0:0.5] {x^\arrayij{mydata}{3}{1}};
\addlegendentry{TD-like Fit for Stra};

\end{axis}

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容