23

Question

I want to attach a label to the curves I draw.

My current solution is to add a node after the plot of my function (see the code below). But, when I explicitly cut out a part of my graph, with the option clip = true, the label can be completely away from the curve (see the picture below).

Do you have an idea on how to resolve this issue, or a different approach to the issue of labeling my curves? I am looking for an automatic solution, if possible.

Picture

enter image description here

Code

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}[domain=0.01:5] \begin{axis} [grid = major, clip = true, clip mode=individual, axis x line = middle, axis y line = middle, xlabel={$x$}, xlabel style={at=(current axis.right of origin), anchor=west}, ylabel={$y$}, ylabel style={at=(current axis.above origin), anchor=south}, domain = 0.01:5, xmin = 0, xmax = 5, enlarge y limits={rel=0.13}, enlarge x limits={rel=0.07}, ymin = -10, ymax = 10, after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}]

\addplot[color=Firebrick2, samples=100, smooth, ultra thick] {x^2} node(endofplotsquare){} ; \node [right, color=Firebrick2] at (endofplotsquare) {$x^2$};

\addplot[color=DodgerBlue2, samples=100, smooth, ultra thick] {exp(x)};

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump, no markers] {ln(x)}; \end{axis}

\end{tikzpicture}

\end{document}

Colas
  • 6,772
  • 4
  • 46
  • 96

2 Answers2

30

You can combine pgfplots' nodes near coords feature with restrict y domain to to avoid the trial-and-error process of finding the last non-clipped point.

By using restrict y to domain=-10:10 (the same values as ymin and ymax) pgfplots discards any points that are not within that interval. Therefore, the last point within the interval will always be pos=1 and nodes can be placed easily like so:

\addplot[color=Firebrick2,samples=100,smooth,ultra thick] {x^2} node[above,pos=1] {$x^2$};

Or like so if you need to save the node position:

\addplot[color=Firebrick2,samples=100,smooth,ultra thick] {x^2} node[pos=1] (endofplotsquare) {};
\node [above,color=Firebrick2] at (endofplotsquare) {$x^2$};

Output:

Output

Solution:

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    grid = major,
    clip = true,
    clip mode=individual,
    restrict y to domain=-10:10,
    axis x line = middle,
    axis y line = middle,
    xlabel={$x$},
    xlabel style={at=(current axis.right of origin), anchor=west},
    ylabel={$y$},
    ylabel style={at=(current axis.above origin), anchor=south},
    domain = 0.01:5,
    xmin = 0,
    xmax = 5,
    enlarge y limits={rel=0.13},
    enlarge x limits={rel=0.07},
    ymin = -10,
    ymax = 10,
    after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};},
  ]

    \addplot[color=Firebrick2,samples=100,smooth,ultra thick] {x^2} node[above,pos=1] {$x^2$};

    \addplot[color=DodgerBlue2,samples=100,smooth,ultra thick] {exp(x)} node[above,pos=1] {$\exp(x)$};

    \addplot[color=Gold3,samples=1000,smooth,ultra thick,unbounded coords=jump,no markers] {ln(x)} node[above,pos=1] {$\ln(x)$};

    %% Alternative %%

    % \addplot[color=Firebrick2,samples=100,smooth,ultra thick] {x^2} node[pos=1] (endofplotsquare) {};
    % \node [above,color=Firebrick2] at (endofplotsquare) {$x^2$};

    % \addplot[color=DodgerBlue2,samples=100,smooth,ultra thick] {exp(x)} node[pos=1] (endofplotsquare) {};
    % \node [above,color=DodgerBlue2] at (endofplotsquare) {$\exp(x)$};

    % \addplot[color=Gold3,samples=1000,smooth,ultra thick,unbounded coords=jump,no markers] {ln(x)} node[pos=1] (endofplotsquare) {};
    % \node [above,color=Gold3] at (endofplotsquare) {$\exp(x)$};
  \end{axis}
\end{tikzpicture}

\end{document}
Ben Voigt
  • 593
sudosensei
  • 4,072
5

This is bit manul and you have to do some trial and error experimentation, but it works. You can use [pos=<fraction>] like

node[pos=0.54](endofplotsquare){} ;

Code:

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}



\begin{tikzpicture}[domain=0.01:5]
\begin{axis}
[grid = major,
clip = true,
clip mode=individual,
axis x line = middle,
axis y line = middle,
xlabel={$x$},
xlabel style={at=(current axis.right of origin), anchor=west},
ylabel={$y$},
ylabel style={at=(current axis.above origin), anchor=south},
domain = 0.01:5,
xmin = 0,
xmax = 5,
enlarge y limits={rel=0.13},
enlarge x limits={rel=0.07},
ymin = -10,
ymax = 10,
after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}]

\addplot[color=Firebrick2, samples=100, smooth, ultra thick] {x^2}
node[pos=0.54](endofplotsquare){} ;
\node [right, color=Firebrick2] at (endofplotsquare) {$x^2$};

\addplot[color=DodgerBlue2, samples=100, smooth, ultra thick] {exp(x)};

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump,  no markers] {ln(x)};
\end{axis}

\end{tikzpicture}

\end{document}

enter image description here