[Tex/LaTex] Label graphs GnuPlot and tikz

gnuplotpgfplots

I'm trying to label some plots but I can't seem to figure it out. I am trying to get something that looks a bit like this:

enter image description here

Found in the texanmple website here. I would like to label the function corresponding to each plot as shown in the figure above.

This is what I have:

\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[plot/.style={very thick,raw gnuplot,mark=none,black}]
    \begin{axis}[
    minor y tick num=2,
    minor x tick num=1,
    extra y ticks={0},
    extra x ticks={0},
    extra tick style={grid style={black},xticklabel=\empty},
    width=0.7\textwidth,
    ymin=-2, ymax=2,
    grid=both, y=1cm,
    axis y line=left,
    axis x line=bottom
    ]
    \addplot gnuplot [plot, samples=500, restrict y to domain=-10:10] {
        plot [-5:5] x**3 - x;
    };

    \addplot gnuplot [plot, blue, samples=500, restrict y to domain=-10:10] {
        plot [-5:5] (x-3)**3 - (x-3);   
    };
    \addplot gnuplot [plot, red, samples=500, restrict y to domain=-10:10] {
        plot [-5:5] (x+3)**3 - (x+3);
    };
\end{axis}\end{tikzpicture}
\end{document}

which produces:

enter image description here

I was hoping to stay in the axis-environment due to styling reasons.

Best Answer

Here's a solution that puts a node positioned along the curve; vary the pos key between 0 (beginning of domain) and 1 (end of domain) as you see fit. You can also play with the anchor, e.g east, west, etc.

enter image description here

% arara: pdflatex: {shell: yes}
\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[plot/.style={very thick,raw gnuplot,mark=none,black}]
    \begin{axis}[
    minor y tick num=2,
    minor x tick num=1,
    extra y ticks={0},
    extra x ticks={0},
    extra tick style={grid style={black},xticklabel=\empty},
    ymin=-2, ymax=2,
    grid=both, y=1cm,
    axis y line=left,
    axis x line=bottom
    ]
    \addplot gnuplot [plot, samples=500, restrict y to domain=-10:10] {
        plot [-2:2] x**3 - x;
        }node[pos=0.7,anchor=west]{$y=f(x)$};

    \addplot gnuplot [plot, blue, samples=500, restrict y to domain=-10:10] {
        plot [1:5] (x-3)**3 - (x-3);   
    }node[pos=0.3,anchor=west]{$y=g(x)$};
    \addplot gnuplot [plot, red, samples=500, restrict y to domain=-10:10] {
        plot [-5:-1] (x+3)**3 - (x+3);
    }node[pos=0.7,anchor=east]{$y=h(x)$};
\end{axis}\end{tikzpicture}
\end{document}

For reference, here's a version without gnuplot

% arara: pdflatex
\documentclass[tikz]{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[plot/.style={very thick,mark=none,black,samples=100}]
    \begin{axis}[
    minor y tick num=2,
    minor x tick num=1,
    extra y ticks={0},
    extra x ticks={0},
    extra tick style={grid style={black},xticklabel=\empty},
    ymin=-2, ymax=2,
    grid=both, y=1cm,
    axis y line=left,
    axis x line=bottom
    ]
    \addplot [plot,domain=-2:2]{x^3 - x}node[pos=0.7,anchor=west]{$y=f(x)$};
    \addplot [plot,blue, domain=1:5] { (x-3)^3 - (x-3)}node[pos=0.3,anchor=west]{$y=g(x)$};
    \addplot [plot,red, domain=-5:-1] {(x+3)^3 - (x+3)}node[pos=0.7,anchor=east]{$y=h(x)$};
\end{axis}
\end{tikzpicture}
\end{document}

Finally, if you want the labels outside the axis, have a look at PgfPlots with labeled plots extend outside the graph box.