# PgfPlots with labeled plots extend outside the graph box

Sorry for the confusing title.

The MWE

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[scale=0.75]
\begin{axis}[ylabel=Y-Axis, xlabel=X-Axis, xmin=0, xmax=10, ymin=0, ymax=12, clip=false, yticklabel pos=right, ylabel near ticks]
\pgfplotsinvokeforeach{0.01,0.1,0.9}{
\addplot[mark=none, domain=0.0:10, thick] {-ln(#1/5^x)/ln(5)} node [pos=0,left] {$c_x=#1$};
}
\end{axis}
\end{tikzpicture}

\end{document}


produces the following graph

This is just what I want... except that the c_x=0.01 line is extending outside of the box!

Setting clip=false is necessary to get the c_x= labels to show up, but I think that it's producing this side-effect.

How can I have my cake and eat it too? That is, how can I have my automagically-positioned labels on the left and my graph nicely contained on the right?

-
One option is to use clip=true, and use pos=0.5, sloped, above option to the node. – Peter Grill Jul 18 '12 at 23:16
Quite so, @PeterGrill. I'll probably go with Jake's answer for this in order to preserve symmetry between several similarly-styled graphs I have for which internal placement of the node would over-crowd the data. But I would upvote your comment as an answer. – Richard Jul 19 '12 at 2:46
I am not quite in the same league as Jake. He is definitely one of the pgfplots guru. – Peter Grill Jul 19 '12 at 4:44

Ah, tricky one!

Here's a new command \sneakylabel{<label text>} that you can use instead of the node [pos=0] ... method.

\pgfplotsinvokeforeach{0.01,0.1,0.9}{
\sneakylabel{$c_x=#1$}
}


will give you

What it does first is position a coordinate at the start of the plot, with a unique name (which uses the label text, so if you need several identical labels, we'll have to tweak this a bit). Then it adds a \node command for the actual label to the after end axis-list, which contains code that will be executed after the clipping has been performed. The node is positioned using the coordinate we saved before.

Enjoy the cake!

\documentclass{standalone}
\usepackage{pgfplots}

\newcommand{\sneakylabel}[1]{
\coordinate (sneakylabel{#1}) at (current plot begin);
\pgfplotsset{
after end axis/.append code={
\node [anchor=east] at (sneakylabel{#1}){#1};
}
}
}

\begin{document}

\begin{tikzpicture}[scale=0.75]
\begin{axis}[ylabel=Y-Axis, xlabel=X-Axis, xmin=0, xmax=10, ymin=0, ymax=12, yticklabel pos=right, ylabel near ticks,]
\pgfplotsinvokeforeach{0.01,0.1,0.9}{
\sneakylabel{$c_x=#1$}

You could use the option restrict y to domain*=0:12.
Unless your sampling points are exactly on the intersection of the line and the axis border, this will introduce a bend in the plot. Also, it will draw the plot along the axis border, so if you use something like red, thick for the plot, you'll be able to see the truncated segment. – Jake Jul 19 '12 at 12:26