计算 TikZ 中节点文本的数值

计算 TikZ 中节点文本的数值

我正在使用tikzpicture环境绘制温度与成分图。我没有确切的数据,所以只是模仿它。

我知道我可以/应该使用pgfplots但这个问题可能会有启发......

我画了两个长度各为 10 个单位的轴(从 0 到 10),并且我想在这些轴上的某些点合成轴刻度。

我想自动计算一个数值并将其放入文本中node

温度的实际计算是T=272.5+2.5*\y 对于组合物,它是\upchi=0.1*\x

我知道如何进行node position计算,但不知道如何计算要放入的数值node text

我可以按照以下代码手动完成此操作:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{upgreek}
\usepackage{siunitx}
\begin{document} 
\begin{tikzpicture}
\draw[step=0.1cm,black!10,very thin] (-0.1,-0.1) grid (10.2,10.2);%setting up grid and axes
\draw[step=1cm,black!50,help lines] (-0.1,-0.1) grid (10.2,10.2);
\draw[thick,->] (0,0) -- (10.2,0);
\draw[thick, ->] (0,0) -- (0,10.2);
\foreach \x in  {0,1,...,10}
\draw[thick] (\x,-2pt) -- (\x,2pt);%drawing tick marks
\foreach \x in  {0,2,...,8}
\draw node at (\x,-10pt) {$0.\x$};% attempt to write node of 0.2, 0.4 etc
%\foreach \x in {0,2,...,10}
%\draw node at (\x, -10pt) {$(0.1*\x)$}; % what I thought I could do...
%\foreach \y in {1,3,...,9}
%\draw node at (-15pt,\y) {$(2.5*\y+272.5)$}; % what I thought I could do...
\draw node at (10,-10pt){$1.0$};
\draw node at (9,-25pt) {Mole Fraction $\upchi_{NB}$};
\draw node at (-15pt, 1) {275};
\draw node at (-15pt, 3) {280};
\draw node at (-15pt, 5) {285};
\draw node at (-15pt, 7) {290};
\draw node at (-15pt, 9) {295};
\draw node at (-40pt, 5)[rotate=90]{Temperature[$\si{\celsius}$]};
\draw node at (5,3.2){2 Phases};
\draw node at (1,6.2)[rotate=0]{1 Phase};
\draw node at (9,7.2)[]{1 Phase};
\foreach \y in {0,1,...,10}
\draw[thick](-2pt,\y) -- (2pt,\y);
\draw[very thick] plot[smooth] coordinates{(0.5,0) (2,5)(4,8) (6,9)(7,8.5)(8.5,5)(9,0) };
\end{tikzpicture}
\end{document}

答案1

您需要先将表达式传递给数学解析器才能对其进行求值。一种方法是使用\pgfmathparse{<expression>},然后使用打印结果\pgfmathprintnumber[<optional number formatting keys>]{\pgfmathresult}

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{siunitx}
\begin{document} 
\begin{tikzpicture}
\draw[step=0.1cm,black!10,very thin] (-0.1,-0.1) grid (10.2,10.2);%setting up grid and axes
\draw[step=1cm,black!50,help lines] (-0.1,-0.1) grid (10.2,10.2);
\draw[thick,->] (0,0) -- (10.2,0);
\draw[thick, ->] (0,0) -- (0,10.2);
\foreach \x in  {0,1,...,10}
\draw[thick] (\x,-2pt) -- (\x,2pt);%drawing tick marks

\foreach \x in {0,2,...,10}
\draw node at (\x, -10pt) {%
    \pgfmathparse{0.1*\x}% Evaluate the expression
    \pgfmathprintnumber[    % Print the result
        fixed,
        fixed zerofill,
        precision=1
    ]{\pgfmathresult}%
};
\foreach \y in {1,3,...,9}
\draw node at (-15pt,\y) {%
    \pgfmathparse{2.5*\y+272.5}%
    \pgfmathprintnumber{\pgfmathresult}%
};
\draw node at (9,-25pt) {Mole Fraction $\chi_{NB}$};
\draw node at (-40pt, 5)[rotate=90]{Temperature[$\si{\celsius}$]};
\draw node at (5,3.2){2 Phases};
\draw node at (1,6.2)[rotate=0]{1 Phase};
\draw node at (9,7.2)[]{1 Phase};
\foreach \y in {0,1,...,10}
\draw[thick](-2pt,\y) -- (2pt,\y);
\draw[very thick] plot[smooth] coordinates{(0.5,0) (2,5)(4,8) (6,9)(7,8.5)(8.5,5)(9,0) };
\end{tikzpicture}
\end{document}

相关内容