2

I would like to generate four hyperbolas in the plane and label them. I'm using the following code (sorry for its length, feel free to shorten it...):

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.9}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    every inner x axis line/.append style={help lines},
    every inner y axis line/.append style={help lines},
    every axis label/.append style={gray},
    axis lines = center,
    xtick = \empty,ytick=\empty,
    xlabel={\tiny$x$},ylabel={\tiny$y$},
    enlargelimits=auto
    ]
    \addplot+[mark=none,red!80!black] function[raw gnuplot] {
      set xrange [-5:5];
      set yrange [-5:5];
      set contour base;
      set cntrparam levels discrete 0.0;
      unset surface;
      set view map;
      set isosamples 70;
      splot x*x/0.5-y*y/1.25-1;
    } node[pos=0.4999,sloped,below]{\tiny$\frac{x^2}{a^2} - \frac{y^2}{b^2} = 1$};

    \addplot+[mark=none,red!90] function[raw gnuplot] {
      set xrange [-5:5];
      set yrange [-5:5];
      set contour base;
      set cntrparam levels discrete 0.0;
      unset surface;
      set view map;
      set isosamples 70;
      splot x*x/0.5-y*y/1.25+1;
    } node[pos=0.95,sloped,below]{\tiny$\frac{x^2}{a^2} - \frac{y^2}{b^2} = -1$};

    \addplot+[mark=none,blue!80!black] function[raw gnuplot] {
      set xrange [-5:5];
      set yrange [-5:5];
      set contour base;
      set cntrparam levels discrete 0.0;
      unset surface;
      set view map;
      set isosamples 70;
      splot x*x/1.25-y*y/0.5-1;
    } node[pos=0.9999,sloped,below]{\tiny$\frac{x^2}{b^2} - \frac{y^2}{a^2} = 1$};

    \addplot+[mark=none,blue!80] function[raw gnuplot] {
      set xrange [-5:5];
      set yrange [-5:5];
      set contour base;
      set cntrparam levels discrete 0.0;
      unset surface;
      set view map;
      set isosamples 70;
      splot x*x/1.25-y*y/0.5+1;
    } node[pos=0.6,sloped,below]{\tiny$\frac{x^2}{b^2} - \frac{y^2}{a^2} = -1$};

  \end{axis}
\end{tikzpicture}
\end{document}

My problem is the placement of the labels. I'm trying to tweak the pos= key, but I never manage to find the right position. I guess that because of its branching behavior it is jumping around unexpectedly. I can only find a satisfying positioning for 3 out of the four. What is the right way to label the hyperbolas? I considered using absolute positioning, but I hope there's a nicer way out.

enter image description here

3
  • Would this help? tex.stackexchange.com/q/142120/34618
    – Jesse
    Commented Nov 4, 2013 at 10:49
  • When writing "absolute positioning" I meant using something like axis cs. I am wondering whether there is some better way.
    – Dror
    Commented Nov 4, 2013 at 10:54
  • 1
    Ya, "absolute positioning" maps immediately to "axis cs" in my head. Did not go through the focal point "But ...." :( -- My bad.
    – Jesse
    Commented Nov 4, 2013 at 11:21

1 Answer 1

4

You can use the approach from pgfplots: Placing node on a specific x-position to place the nodes at particular x and y locations along the plots:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.9}

\usetikzlibrary{intersections}
\begin{document}

\makeatletter
\def\parsenode[#1]#2\pgf@nil{%
    \tikzset{label node/.style={#1}}
    \def\nodetext{#2}
}

\tikzset{
    add node at x/.style 2 args={
        name path global=plot line,
        /pgfplots/execute at end plot visualization/.append={
                \begingroup
                \@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
            \path [name path global = position line #1-1]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [xshift=1pt, name path global = position line #1-2]
                ({axis cs:#1,0}|-{rel axis cs:0,0}) --
                ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [
                name intersections={
                    of={plot line and position line #1-1},
                    name=left intersection
                },
                name intersections={
                    of={plot line and position line #1-2},
                    name=right intersection
                },
                label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node]{\nodetext};
            \endgroup
        }
    },
    add node at y/.style 2 args={
        name path global=plot line,
        /pgfplots/execute at end plot visualization/.append={
                \begingroup
                \@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
            \path [name path global = position line #1-1]
                ({axis cs:0,#1}-|{rel axis cs:0,0}) --
                ({axis cs:0,#1}-|{rel axis cs:1,1});
            \path [yshift=1pt, name path global = position line #1-2]
                ({axis cs:0,#1}-|{rel axis cs:0,0}) --
                ({axis cs:0,#1}-|{rel axis cs:1,1});
            \path [
                name intersections={
                    of={plot line and position line #1-1},
                    name=left intersection
                },
                name intersections={
                    of={plot line and position line #1-2},
                    name=right intersection
                },
                label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node] {\nodetext};
            \endgroup
        }
    }
}


\begin{tikzpicture}
  \begin{axis}[
    every inner x axis line/.append style={help lines},
    every inner y axis line/.append style={help lines},
    every axis label/.append style={gray},
    axis lines = center,
    xtick = \empty,ytick=\empty,
    xlabel={\tiny $x$},ylabel={\tiny $y$},
    enlargelimits=auto
    ]
    \addplot+[mark=none,red!80!black,
add node at y={-4}{[red!80!black, sloped, above, inner sep=1pt]{\tiny $\frac{x^2}{a^2} - \frac{y^2}{b^2} = 1$}}        
    ] function[raw gnuplot] {
      set xrange [-5:5];
      set yrange [-5:5];
      set contour base;
      set cntrparam levels discrete 0.0;
      unset surface;
      set view map;
      set isosamples 70;
      splot x*x/0.5-y*y/1.25-1;
    };

    \addplot+[mark=none,red!90,
        add node at y={3}{[red!90, sloped, above, inner sep=1pt]{\tiny $\frac{x^2}{a^2} - \frac{y^2}{b^2} = -1$}}    
    ] function[raw gnuplot] {
      set xrange [-5:5];
      set yrange [-5:5];
      set contour base;
      set cntrparam levels discrete 0.0;
      unset surface;
      set view map;
      set isosamples 70;
      splot x*x/0.5-y*y/1.25+1;
    };

    \addplot+[mark=none,blue!80!black,
        add node at x={-4}{[blue!80!black, sloped, below, inner sep=1pt]{\tiny $\frac{x^2}{b^2} - \frac{y^2}{a^2} = 1$}}        
        ] function[raw gnuplot] {
      set xrange [-5:5];
      set yrange [-5:5];
      set contour base;
      set cntrparam levels discrete 0.0;
      unset surface;
      set view map;
      set isosamples 70;
      splot x*x/1.25-y*y/0.5-1;
    };

    \addplot+[mark=none,blue!80,
add node at x={4}{[blue!80, sloped, above, inner sep=1pt]{\tiny $\frac{x^2}{b^2} - \frac{y^2}{a^2} = -1$}}    
    ] function[raw gnuplot] {
      set xrange [-5:5];
      set yrange [-5:5];
      set contour base;
      set cntrparam levels discrete 0.0;
      unset surface;
      set view map;
      set isosamples 70;
      splot x*x/1.25-y*y/0.5+1;
    };

  \end{axis}
\end{tikzpicture}
\end{document}
2
  • It is indeed working, but still somehow fuzzy. For example, in the 3rd curve, changing the -4 with -3.5 yields an unexpected(?) result. Can you explain this? I believe it has to do with the fact that this branch is not an x to y function. A way out, could be to split it into two branches; one above and one below the x axis.
    – Dror
    Commented Nov 5, 2013 at 7:54
  • Can you explain what \pgf@nil is and where is it defined? And, is it an illusion or is there one extra (unbalanced) }?
    – Dror
    Commented Nov 5, 2013 at 8:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .