5

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.

cmhughes
  • 100,947
breeden
  • 377
  • 1
  • 9
  • 1
    Try adding, eg, node{$y=f(x)$} just before the semicolons – cmhughes Feb 22 '16 at 19:42
  • @cmhughes, can you please specify which semicolons? I'm trying this and it does not work for me... – Ondrian Feb 22 '16 at 19:46
  • The second answer might be helpful: http://tex.stackexchange.com/questions/172842/how-to-label-the-curve-of-a-function-at-any-point-along-it-in-tikz – Marco Feb 22 '16 at 19:47
  • @Marco, I think you are right. I will try to play around with this some more. I'm not sure what the difference is between \addplot and \addplot gnuplot. I know with the \addplot (without gnuplot) I need to write x**3 - x as x^3 - x instead. – breeden Feb 22 '16 at 19:59
  • I hope I've cleared it up in my answer :) – cmhughes Feb 22 '16 at 20:11

2 Answers2

2

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.

cmhughes
  • 100,947
  • 1
    This is a very helpful solution. Thank you. I wasnt able to understand the "pos=" argument until now. Let me play around with it this evening. It doesn't appear that placing the labels outside the graph will be worth the hassle. – breeden Feb 22 '16 at 20:13
0

You can add a legend to the pgfplot axis as follows. Is this enough? Or are you after the colouring of the legend text?

\documentclass[tikz,border=10pt]{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,
    legend style={at={(1.1,1)}, anchor={north west}},
    ]
    \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);
    };
    \legend{$ x^3 - x $,$ (x-3)^3 - (x-3) $,$ (x+3)^3 - (x+3) $}
    \end{axis}\end{tikzpicture}
\end{document}

enter image description here

Ondrian
  • 1,536
  • Ondrian, I was hoping to avoid a legend, but it may have to do. The problem is that the plot may be printed in grayscale which may make the legend difficult to interpret. If that's the case though, I may write a grayscale flag that will pick more grayscale-color friendly tones. – breeden Feb 22 '16 at 19:48
  • Ok, I think it is manageable to do this. I'll try to figure it out. – Ondrian Feb 22 '16 at 19:52