3

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]
    \addplot gnuplot {sin(x)};

    %Fix bounding box from this point; do not add any of the following to the bounding box.
    \addplot[overlay] gnuplot {exp(x)};
  \end{axis}
\end{tikzpicture}


\end{document}
5

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,
        trig format plots=rad,
        restrict y to domain=-1:10,
    ]
    \addplot {sin(x)};

    \addplot+[update limits=false] {exp(x)};
    \end{axis}
\end{tikzpicture}


\end{document}

enter image description here

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,
        trig format plots=rad,
        restrict y to domain=-1:10,
    ]
    \addplot {sin(x)};

    \begin{pgfplotsinterruptdatabb}
    \addplot {exp(x)};
    \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,
        trig format plots=rad,
        restrict y to domain=-1:10,
        hide axis,
    ]
    \addplot {sin(x)};

    \addplot+[overlay] {exp(x)};
    \end{axis}
\end{tikzpicture}}


\end{document}

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.