8

I am using pgfplot to generate a family of curves. I would like to annotate each curve with the appropriate parameter as specified by foreach variable (in this case temperatureRatio). However I get an error when Undefined control sequence when I specify the label to temperatureRatio in the line ... node[right, pos=1, font=\small]{\temperatureRatio};. How can I fix this?

\documentclass{article}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{pgfplots}
    \pgfplotsset{width=8cm,compat=newest}

\begin{document}

\noindent
\begin{minipage}{\linewidth}
    \centering
    \begin{tikzpicture}[scale=1]
    \begin{axis}[ xmin=0, xmax=1,   ymin=0, ymax=5,
                  xlabel={$\eta$},   ylabel={$\mathbb{P} = \dot{W} /(\dot{m} C_p T_1)$}
                ]
        \foreach \temperatureRatio in {4,...,10}
        {
            \addplot[domain=10:50, samples = 41, samples y = 0, blue]
            ({ 1 - x^(-0.2857) }, { (1 - x^(-0.2857))*(\temperatureRatio - x^(0.2857))})
            node[right, pos=1, font=\small]{\temperatureRatio};
        }

        \addplot[domain = 3:10, samples = 20, samples y = 0, red]
        ({1 - x^(-1/2)},{(sqrt(x)-1)^2})
        node[left,pos=0]{$\mathbb{P}_{\text{max}}$};

    \end{axis}
    \end{tikzpicture}
\end{minipage}

\end{document}
7

pgfplots has different foreach macros with different characteristics. Here you have to use \pgfplotsinvokeforeach.

\documentclass{article}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{pgfplots}
    \pgfplotsset{width=8cm,compat=newest}

\begin{document}

\noindent
\begin{minipage}{\linewidth}
    \centering
    \begin{tikzpicture}[scale=1]
    \begin{axis}[ xmin=0, xmax=1,   ymin=0, ymax=5,
                  xlabel={$\eta$},   ylabel={$\mathbb{P} = \dot{W} /(\dot{m} C_p T_1)$}
                ]
        \pgfplotsinvokeforeach{4,...,10}
        {
            \addplot[domain=10:50, samples = 41, samples y = 0, blue]
            ({ 1 - x^(-0.2857) }, { (1 - x^(-0.2857))*(#1 - x^(0.2857))})
            node[right, pos=1, font=\small]{#1};
        }

        \addplot[domain = 3:10, samples = 20, samples y = 0, red]
        ({1 - x^(-1/2)},{(sqrt(x)-1)^2})
        node[left,pos=0]{$\mathbb{P}_{\text{max}}$};

    \end{axis}
    \end{tikzpicture}
\end{minipage}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.