# Consistent Pgfplot Labels Positions with/without Gnuplot

I like that I can place a node at the end of plot and the node appears relative to the last point on the graph. When the domain is specified in increasing order \DomainMin:\DomainMax, the label gets placed exactly where I want it -- both with, and without gnuplot (see Graphs 1 and 2):

However, when the domain is specified in decreasing order DomainMax:\DomainMin, I get the label where I want it if I use PGF to compute the coordinates (See Graph 3), but it is in the wrong place when I use gnuplot (See Graph 4). I suspect this is because gnuplot ignores the order the start and end of the domain are specified and computes coordinates in increasing x order.

I tried to use pos=0 thinking that it would place the label at the point I want, but this not quite the case -- it appears quite a bit lower, but don't know why.

So, is there a way I can use gnuplots place the label the same place that pgfplots places it when I specify the domain in decreasing order?

Here is the code that produces the above 5 graphs:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}

\newcommand*{\XAxisMin}{-1.0}
\newcommand*{\XAxisMax}{3.0}
\newcommand*{\YAxisMin}{-2.0}
\newcommand*{\YAxisMax}{10}

\newcommand*{\DomainMinF}{\XAxisMin}
\newcommand*{\DomainMaxF}{2.2}
\newcommand*{\PlotLabel}{$y=x^3$}%

\pgfkeys{/pgfplots/Axis Style/.style={
clip=false,% so that we can see labels placed outside
xmin=\XAxisMin, xmax=\XAxisMax,
ymin=\YAxisMin, ymax=\YAxisMax,
width=6.5cm
}}

% Gnuplot options here  have no effect if not using GnuPlot
\pgfkeys{/pgfplots/Plot Style/.style={
translate gnuplot=true,% can use ‘^’ instead of ‘**’
id=foo,
mark=none,%
domain=\DomainMinF:\DomainMaxF,%
samples=50,%
ultra thick,
}}

\newcommand*{\AddLabel}[1]{\node [align = center] at (axis cs: 0.4,5) {#1};}%

\begin{document}
With increasing domain=$\DomainMinF:\DomainMaxF$, we get label at the end of the graph as desired:

\begin{tikzpicture}
\begin{axis}[Axis Style]
{(x)^3} node [right] {\PlotLabel};%
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[Axis Style]
gnuplot% Use Gnuplot for Graph 2
{(x)^3} node [right] {\PlotLabel};%
\end{axis}
\end{tikzpicture}

\bigskip
But, with decreasing domain=$\DomainMaxF:\DomainMinF$, we get the label in the wrong spot with Gnuplot:

\begin{tikzpicture}
\begin{axis}[Axis Style]
{(x)^3} node [right] {\PlotLabel};%
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[Axis Style]
gnuplot% Use Gnuplot for Graph 4
{(x)^3} node [right] {\PlotLabel};% adding pos=0.0 does not work
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[Axis Style]
gnuplot% Use Gnuplot for Graph 5
{(x)^3} node [right, pos=0.0] {\PlotLabel};
\end{axis}
\end{tikzpicture}
\end{document}

• This is really similar to my question Label plots in pgfplots without entering coordinates manually: The development version of PGFplots supports placing nodes using pos. – Jake Oct 26 '11 at 5:26
• @Jake: Definitely a related/similar question. I did have a look at that but was not able to get linelabel to work. I can try to recreate it and post as a separate question if you like. But, I normally either label at either end of the plot, or manually place the label independent of the plot. – Peter Grill Oct 26 '11 at 5:32

The easiest thing to do is probably to upgrade to the current version of PGFplots (>=1.5.1), which allows nodes to be placed along plots using [pos=...].

Alternatively, you could use the plotlabel style that I used in my answer to Label plots in pgfplots without entering coordinates manually. It allows you to place nodes at specified distances along the path. The syntax is plotlabel{<pos>}{<label text>}. This uses the markings decoration, which uses pgfnode instead of TikZ nodes, which isn't quite as flexible.

And thirdly, here's a trimmed down version of the linelabel style that I used in my answer to Label plots in pgfplots without entering coordinates manually. This has the syntax linelabel={<relative horizontal position along axis>}{<label code>}, where <label code> will be passed to a label (see p. 194 of the pgfmanual), so you can use something like [node options]<placement>:<label text>:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usetikzlibrary{decorations.markings}
\makeatletter
\tikzset{
nomorepostaction/.code={\let\tikz@postactions\pgfutil@empty},
plotlabel/.style 2 args={
every path/.append style={
postaction={
nomorepostaction,
decorate,
decoration={
markings,
mark=at position #1 with {
\pgftransformresetnontranslations
\pgfnode{rectangle}{west}{#2}{}{}
}
}
}
}
}
}
\makeatother

\newcommand*{\XAxisMin}{-1.0}
\newcommand*{\XAxisMax}{3.0}
\newcommand*{\YAxisMin}{-3.0}
\newcommand*{\YAxisMax}{10}

\newcommand*{\DomainMinF}{\XAxisMin}
\newcommand*{\DomainMaxF}{2.2}
\newcommand*{\PlotLabel}{$y=x^3$}%

\pgfkeys{/pgfplots/Axis Style/.style={
clip=false,% so that we can see labels placed outside
xmin=\XAxisMin, xmax=\XAxisMax,
ymin=\YAxisMin, ymax=\YAxisMax,
width=6.5cm
}}

% Gnuplot options here  have no effect if not using GnuPlot
\pgfkeys{/pgfplots/Plot Style/.style={
translate gnuplot=true,% can use ‘^’ instead of ‘**’
id=foo,
mark=none,%
domain=\DomainMinF:\DomainMaxF,%
samples=50,%
ultra thick,
}}

\pgfkeys{/pgfplots/linelabel/.style 2 args={name path global=labelpath,execute at end plot={
\path [name path global = labelpositionline]
(rel axis cs:#1,0) --
(rel axis cs:#1,1);
\draw [name intersections={of=labelpath and labelpositionline}] (intersection-1)  node [label={#2}] {};},
}}

\newcommand*{\AddLabel}[1]{\node [align = center] at (axis cs: 0.4,5) {#1};}%

\begin{document}
\begin{tikzpicture}
\begin{axis}[Axis Style]
{(x)^3};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[Axis Style]
gnuplot {(x)^3};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[Axis Style]
{(x)^3};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[Axis Style]

• This does appear to solve the problem for this specifc case, but I would prefer the label at where the left end of the graph is. So, for example if the setting had been \newcommand*{\DomainMinF}{0.0} this won't work as there is no intersection. I was thinking that if the first coordinate could be extracted from the gnuplot file, and the node placed at that point – Peter Grill Oct 26 '11 at 6:04
• I thought that I could use linelabel={\DomainMinF}..., but that too did not work. Also, I don't quite understand the syntax of the 2nd parameter to linelabel where the color is in [], but the position is outside? – Peter Grill Oct 26 '11 at 6:09
• I've added the second approach from the answer I linked to, which allows you to specify a pos along the label path. The syntax of the second parameter is explained in the section on labels in the pgfmanual. – Jake Oct 26 '11 at 9:30