将文本文件中的数字添加到轴标签

将文本文件中的数字添加到轴标签

我有一堆独立的 tikz 图像,需要在轴标签上添加一个数字。我添加到每个标签的数字位于一个文本文件中,该文件可能会根据我使用的数据集而变化。有没有办法读取包含此数字的文本文件并将其插入 y 轴标签中?其中 XXX.XX 是我从文本文件中读取的值。

示例文本文件:Wall_Tempartures.txt

在此处输入图片描述

\documentclass[class=minimal,border=0pt]{standalone}
\usepackage{color}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepackage[condensed,light]{roboto}    % Add font for figures
\usepackage[T1]{fontenc}                % Specify font encoding
\usepackage{mathptmx}

%Define colors for plotting
\definecolor{darkgreen}{rgb}{0.125,0.500,0.169}
\definecolor{darkred}  {rgb}{0.900,0.100,0.100}
\tikzset{>=latex}   % Change default arrow tip style

\newcommand{\labelsize}{\fontsize{9pt}{9pt}\selectfont}
\newcommand{\footnotesize}{\fontsize{7pt}{7pt}\selectfont}
\newcommand{\footfootnotesize}{\fontsize{5pt}{5pt}\selectfont}
\newcommand{\linewidths}{0.75pt}
\newcommand{\errormarksize}{3pt}

\begin{document}

\begin{tikzpicture}
    \pgfplotsset{every axis legend/.append style={at={(0.5,0.03)},anchor=south,nodes=right},
    legend style={font={\footnotesize}}}

    \begin{axis}[name=topplot,clip mode=individual,
        axis background/.style={fill=white},
        width=2.5in,height=2.5in,
        xmin=0,xmax=2000,
        ymin=-0.01,ymax=0.03,
        tick scale binop=\times,
        minor tick num=4,
        legend columns=2,
        xlabel={$z$-Position, [mm]},
        ytick pos=both,
        ylabel={\labelsize $\theta = \frac{T-T_\textrm{\footfootnotesize amb}}{T_\textrm{\footfootnotesize amb}}$, $\left[ \frac{\Delta \textrm{\footnotesize K}}{\textrm{\footnotesize K}} \right]$, $T_\textrm{\footfootnotesize amb}$= XXX.XX K},
        scaled y ticks=false,
        y tick label style={
            /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=2,
            /tikz/.cd}]

        \addplot+[blue,only marks,mark=o,line width=\linewidths,mark size=1.5,
                error bars/.cd,
                    y dir=both,
                    y explicit,
                    error bar style={line width=\linewidths},
                    error mark options={rotate=90,mark size=\errormarksize,
                    line width=\linewidths}]
            table[x index=0,y index=1,y error index=13] {Wall_Temperatures.txt};
            \addlegendentry{Top Wall}
        \addplot+[red,only marks,mark=square,line width=\linewidths,mark size=1.5,
                error bars/.cd,
                    y dir=both,
                    y explicit,
                    error bar style={line width=\linewidths},
                    error mark options={rotate=90,mark size=\errormarksize,
                    line width=\linewidths}]
            table[x index=0,y index=4,y error index=16] {Wall_Temperatures.txt};
            \addlegendentry{Left Wall}
        \addplot+[darkgreen,only marks,mark=triangle,line width=\linewidths,mark size=1.5,
                error bars/.cd,
                    y dir=both,
                    y explicit,
                    error bar style={line width=\linewidths},
                    error mark options={rotate=90,mark size=\errormarksize,
                    line width=\linewidths}]
            table[x index=0,y index=7,y error index=19] {Wall_Temperatures.txt};
            \addlegendentry{Plate}
        \addplot+[violet,only marks,mark=x,line width=\linewidths,mark size=2.0,
                error bars/.cd,
                    y dir=both,
                    y explicit,
                    error bar style={line width=\linewidths},
                    error mark options={rotate=90,mark size=\errormarksize,
                    line width=\linewidths}]
            table[x index=0,y index=10,y error index=22] {Wall_Temperatures.txt};
            \addlegendentry{Right Wall} 

\end{axis}

\end{tikzpicture}

\end{document}

答案1

这里有个建议。首先借助catchfile包将temperaturefile的内容读入宏中:

\usepackage{catchfile}
\CatchFileDef{\AT}{ambtemp.txt}{}

添加\usepackage{siunitx}并使用它可以解析并打印带单位的数字,例如\SI[round-mode=places,round-precision=2]{\AT}{\kelvin}。请参阅手册以siunitx了解所有可能的选项。

在此处输入图片描述

\documentclass[class=minimal,border=0pt]{standalone}
% for number parsing and units
\usepackage{siunitx}

\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}

% I don't have this one
%\usepackage[condensed,light]{roboto}    % Add font for figures
\usepackage[T1]{fontenc}                % Specify font encoding
\usepackage{mathptmx}

% just for example, writes a number to a file
\usepackage{filecontents}
\begin{filecontents*}{ambtemp.txt}
273.947572094827
\end{filecontents*}
% read in ambient temperature and save in macro \AT
\usepackage{catchfile}
\CatchFileDef{\AT}{ambtemp.txt}{}

%Define colors for plotting
\definecolor{darkgreen}{rgb}{0.125,0.500,0.169}
\definecolor{darkred}  {rgb}{0.900,0.100,0.100}
\tikzset{>=latex}   % Change default arrow tip style

\newcommand{\labelsize}{\fontsize{9pt}{9pt}\selectfont}
\newcommand{\footnotesize}{\fontsize{7pt}{7pt}\selectfont}
\newcommand{\footfootnotesize}{\fontsize{5pt}{5pt}\selectfont}
\newcommand{\linewidths}{0.75pt}
\newcommand{\errormarksize}{3pt}

\begin{document}

\begin{tikzpicture}
    \pgfplotsset{every axis legend/.append style={at={(0.5,0.03)},anchor=south,nodes=right},
    legend style={font={\footnotesize}}}

    \begin{axis}[name=topplot,clip mode=individual,
        axis background/.style={fill=white},
        width=2.5in,height=2.5in,
        xmin=0,xmax=2000,
        ymin=-0.01,ymax=0.03,
        tick scale binop=\times,
        minor tick num=4,
        legend columns=2,
        xlabel={$z$-Position, [mm]},
        ytick pos=both,
        ylabel={\labelsize $\theta = \frac{T-T_\textrm{\footfootnotesize amb}}{T_\textrm{\footfootnotesize amb}}$, $\left[ \frac{\Delta \textrm{\footnotesize K}}{\textrm{\footnotesize K}} \right]$, $T_\textrm{\footfootnotesize amb}= \SI[round-mode=places,round-precision=2]{\AT}{\kelvin}$},
        scaled y ticks=false,
        y tick label style={
            /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=2,
            /tikz/.cd}]

        \addplot+[blue,only marks,mark=o,line width=\linewidths,mark size=1.5,
                error bars/.cd,
                    y dir=both,
                    y explicit,
                    error bar style={line width=\linewidths},
                    error mark options={rotate=90,mark size=\errormarksize,
                    line width=\linewidths}]
            table[x index=0,y index=1,y error index=13] {Wall_Temperatures.txt};
            \addlegendentry{Top Wall}
        \addplot+[red,only marks,mark=square,line width=\linewidths,mark size=1.5,
                error bars/.cd,
                    y dir=both,
                    y explicit,
                    error bar style={line width=\linewidths},
                    error mark options={rotate=90,mark size=\errormarksize,
                    line width=\linewidths}]
            table[x index=0,y index=4,y error index=16] {Wall_Temperatures.txt};
            \addlegendentry{Left Wall}
        \addplot+[darkgreen,only marks,mark=triangle,line width=\linewidths,mark size=1.5,
                error bars/.cd,
                    y dir=both,
                    y explicit,
                    error bar style={line width=\linewidths},
                    error mark options={rotate=90,mark size=\errormarksize,
                    line width=\linewidths}]
            table[x index=0,y index=7,y error index=19] {Wall_Temperatures.txt};
            \addlegendentry{Plate}
        \addplot+[violet,only marks,mark=x,line width=\linewidths,mark size=2.0,
                error bars/.cd,
                    y dir=both,
                    y explicit,
                    error bar style={line width=\linewidths},
                    error mark options={rotate=90,mark size=\errormarksize,
                    line width=\linewidths}]
            table[x index=0,y index=10,y error index=22] {Wall_Temperatures.txt};
            \addlegendentry{Right Wall} 

\end{axis}

\end{tikzpicture}

\end{document}

相关内容