# Fix bounding box in pgfplots

I want to do the following:

Do some draw or plot commands in pgfplots and then fix the bounding box. I.e. all plots which come afterwards shouldn't influence the bounding box. How can I do this? I tried overlay, but it doesn't work:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[no markers]

%Fix bounding box from this point; do not add any of the following to the bounding box.
\end{axis}
\end{tikzpicture}

\end{document}


Use update limits=false. Modifying the bounding box is also possible, but not what you want in this context -- you want to change the entire appearance of the axis (unless I am mistaken). A solution is

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
no markers,
restrict y to domain=-1:10,
]

\end{axis}
\end{tikzpicture}

\end{document}


Note that I had to add restrict y to domain due to an internal weakness with the floating point arithmetics in pgfplots.

Alternatively, you can use an environment to disable updates to the axis limits. This may be more effective in order apply the modification to "all following plots":

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
no markers,
restrict y to domain=-1:10,
]

\begin{pgfplotsinterruptdatabb}
\end{pgfplotsinterruptdatabb}
\end{axis}
\end{tikzpicture}

\end{document}


Note that overlay addresses a different use-case: it excludes graphical elements from the bounding box only, but it does not affect how axis limits are chosen. Thus, overlay instead of update limits=false has no effect since the axis limits would still be chosen as before, and since the axis lines define the bounding box, the overlay feature has no effect.

However, overlay has an effect if you hide the axis lines:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\begin{document}

This is the figure and its BB:

\fbox{%
\begin{tikzpicture}
\begin{axis}[
no markers,
restrict y to domain=-1:10,
hide axis,
]

\end{axis}
\end{tikzpicture}}

\end{document}


Note that both figures have been respected during the axis limit computation (which can be seen by the fact that the y axis is scaled down). But the second plot does not affect the bounding box as can be seen by the \fbox command; it even crosses the text.

Thus: update limits=false controls how a single plot affects the axis limits (which has implications for the scaling and the descriptions). The overlay key is useful if it comes to the graphical elements and their bounding box.

• Thanks, works as expected. Could you add an explanation about the difference of fixing the bounding box? – student Nov 15 '15 at 11:05
• I have edited my answer to illustrate the changes – Christian Feuersänger Nov 15 '15 at 14:39
• Just a stupid question, but couldn't you have used ymin and ymax to do the same? – daleif Nov 15 '15 at 17:37
• Yes, right. But "use it" means manual work whereas 'update limits=false' does this automatically - for selected plots. – Christian Feuersänger Nov 15 '15 at 21:25