2

I am trying to plot a figure for different parameter values on the same axis. I have this code:

\documentclass[a4paper,11pt,oneside,onecolumn]{article}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}
\begin{document}
\begin{center}
\begin{tikzpicture}[baseline]
\begin{axis}[
axis y line=left,
axis x line=bottom,
grid=none,
xmin=0,xmax=0.8,
ymin=0,ymax=1.2,
xlabel=$\frac{PX}{E^2}$,ylabel=$\frac{QX}{E^2}$,
width=0.9\columnwidth,
height=0.45\columnwidth,
xtick={0,0.2,...,0.8},
ytick={0,0.2,...,1.2},
anchor=center,
y tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=1,
    /tikz/.cd
},
x tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=1,
    /tikz/.cd
}
]
\foreach \tf in {
    -0.2,
    0,
    0.2%
} {%
    \addplot[domain=0:0.8,samples=2000
    ] {sqrt(1/2-x*\tf+sqrt(1/4-x^2-x*\tf))} node[right] {$tan\phi=$\tf} ;
    \addplot[domain=0:0.8,samples=2000
    ] {sqrt(1/2-x*\tf-sqrt(1/4-x^2-x*\tf))} ;
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

It plots the curves nicely but I have two problems. First, I get an error on \end{axis}:

Undefined control sequence. \end{axis}
Undefined control sequence. \end{axis}
Undefined control sequence. \end{axis}

Yes, it's three times which I guess relates to the 3 loops I have.

Second, the label doesn't show up properly. The \tf works fine in the plots but is ignored in the label.

This is what I get:

enter image description here

Any help appreciated.

  • i can't obtain your result. if i replace \tf with \i, then i det similar result, bit all notes has the same content: \tan\phi=1 (and not as you probably expected \tan\phi=-0.2, \tan\phi=0 and \tan\phi=0.2 ... the simplest solution is not use \foreach loop or use legend instead of nodes. anyway, number of samples is far to big. with samples=21 you will obtain better image. – Zarko Dec 7 '17 at 13:29
1

Loops inside an axis are a bit tricky, cf. section 8.1 Utility commands in the pgfplots manual. But pgfplots as another macro for loops, \pgpflotsinvokeforeach, which works in this case:

output of code

\documentclass[a4paper,11pt,oneside,onecolumn]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{center}
\begin{tikzpicture}[baseline]
\begin{axis}[
axis y line=left,
axis x line=bottom,
grid=none,
xmin=0,xmax=0.8,
ymin=0,ymax=1.2,
xlabel=$\frac{PX}{E^2}$,ylabel=$\frac{QX}{E^2}$,
width=0.9\columnwidth,
height=0.45\columnwidth,
xtick={0,0.2,...,0.8},
ytick={0,0.2,...,1.2},
anchor=center,
y tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=1,
    /tikz/.cd
},
x tick label style={
    /pgf/number format/.cd,
    fixed,
    fixed zerofill,
    precision=1,
    /tikz/.cd
}
]
\pgfplotsinvokeforeach{-0.2,0,0.2}{%
    \addplot[domain=0:0.8,samples=2000
    ] {sqrt(1/2-x*#1+sqrt(1/4-x^2-x*#1))} node[right] {$\tan\phi=#1$} ;
    \addplot[domain=0:0.8,samples=2000
    ] {sqrt(1/2-x*#1-sqrt(1/4-x^2-x*#1))} ;
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

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.