24

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}
1
  • How to change the size of the node's text?
    – minseong
    Sep 20, 2017 at 18:55

2 Answers 2

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}
5
  • 2
    Domo Arigato Gozaimashta @sudosensei ;-)
    – Colas
    Apr 8, 2014 at 14:58
  • How do I change the size of the node text?
    – minseong
    Sep 20, 2017 at 18:55
  • 1
    @theonlygusti Have you tried something like node[above,pos=1,font=\Large]. I can't test this right now.
    – sudosensei
    Sep 21, 2017 at 10:02
  • @sudosensei I tried \small but it wasn't very small. I want it very small
    – minseong
    Sep 21, 2017 at 11:58
  • @theonlygusti \tiny is the smallest font size as far as I know. sharelatex.com/learn/…
    – sudosensei
    Sep 21, 2017 at 15:57
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

You must log in to answer this question.

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