4

I am trying to overlay a pgfplots produced graph with other curves within an animation using the same plotting domain, but suppressing any output (axes, labels, ticks) other than the curves to be overlaid.

For this to work properly, the bounding boxes of the overlay graphs and the graph positions should match the original plot.

Is there a way to "phantomize" the unwanted parts of a plot? Ideally not by dyeing them "white" or manipulating the opacity value.

This simple plot may serve as template:

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    % hide axis, % this changes the bounding box
    domain=0:360,
    ymax=1.1,ymin=-1.1
  ]
    \addplot {sin(x)};
  \end{axis}
\end{tikzpicture}
\end{document}
AlexG
  • 54,894

2 Answers2

3

Important to note is that since PGFPlots v1.8 the behavior of how the bounding box (BB) is caluclated has changed (see the description of hide axis in the PGFPlots manual (v1.13)). After setting this to the "old behavior" it is pretty much the same as in the answer I haven given here.

For more details see the comments in the code.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{
        pgfplots.external,
    }
    \pgfdeclarelayer{background}
    \pgfsetlayers{background,main}
    \tikzset{
        % define a style to apply to each `tikzpicture' to visualize the
        % bounding box by setting a fill color in the background of the
        % bounding box
        % (comment everything in the style if you are sure the bounding box
        % fits everything)
        BB style/.style={
            execute at end picture={
                \begin{pgfonlayer}{background}
                    \path [
                        fill=yellow,
                    ]
                        (current bounding box.south west)
                        rectangle
                        (current bounding box.north east);
                \end{pgfonlayer}
            },
        },
    }
    \pgfplotsset{
        compat=1.13,
        %
        % define a style which should apply to both plots, so values don't
        % change, when you change e.g. the function
        bla bla style/.style={
            % only the axis should be scaled
            scale only axis,
            % set width and height of the plots
            width=\axisdefaultwidth,
            height=\axisdefaultheight,
            % define axis limits
            xmin=0,
            xmax=360,
            ymin=-1,
            ymax=1,
            domain=0:360,
            enlargelimits=true,
            % don't calculate bounding box
            overlay,
            % disable clipping of the (invisible) axes
            clip bounding box=default tikz,
            %compat/BB=1.7, % <-- this is equivalent to the above key-value
        },
    }
%    % `shell-escape' feature needs to be enabled so
%    % image externalization works "automatically"
%    \tikzexternalize[
%        % only externalize pictures which are explicitly named
%        only named=true,
%        % set a path here, to where the pictures and plots should be externalized
%        prefix=Pics/pgf-export/,
%%        % uncomment me to force image externalization
%%        % (in case you didn't change anything in the `tikzpicture' environments
%%        % itself which would lead to an externalization, too
%%        force remake=true,
%    ]
    % define a command to set the bounding box
    \newcommand*{\UseAsBB}{
        \useasboundingbox
            % adjust the `shift' values to the bounding box
            % so all stuff is included
            ([shift={(-10mm,-5mm)}] current axis.south west)
            rectangle
            ([shift={(1mm,1mm)}] current axis.north east);
    }
\begin{document}
    \tikzsetnextfilename{full_plot}
    \begin{tikzpicture}[
        BB style,
    ]
        \begin{axis}[
            bla bla style,
        ]
            \addplot {sin(x)};
        \end{axis}
        % use defined command to set the bounding box
        \UseAsBB
    \end{tikzpicture}

    \tikzsetnextfilename{curve_plot}
    \begin{tikzpicture}[
        BB style,
    ]
        \begin{axis}[
            bla bla style,
            % hide the axes
            hide axis,
        ]
            \addplot {sin(x)};
        \end{axis}
        % use defined command to set the bounding box
        \UseAsBB
    \end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
  • you can use overlay instead of the environment – percusse Feb 28 '16 at 22:36
  • @percusse, of course. So far I only used it in combination with remember picture and I think this combination is "hard-wired" in my brain. Thank you very much. I adapted the code. – Stefan Pinnow Feb 29 '16 at 06:40
0

After experimenting with Stefan's code, it boils down to saving the bounding box coordinates of the "master" plot and to re-use them in the subsequent plots and to hide axes with hide axis.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}

\begin{document} \begin{tikzpicture} \begin{axis}[ domain=0:360, xmin=0, xmax=360, ymin=-1, ymax=1, ] \addplot {sin(x)}; \end{axis} % save bounding box coordinates \coordinate (ll) at (current bounding box.south west); \coordinate (ur) at (current bounding box.north east); \end{tikzpicture}

\begin{tikzpicture}
    % use saved coordinates for setting the bounding box
    \useasboundingbox (ll) rectangle (ur);
    \begin{axis}[
        hide axis,% hide the axes
        domain=0:360,
        xmin=0, xmax=360,
        ymin=-1, ymax=1,
    ]
        \addplot {sin(x)};
    \end{axis}
\end{tikzpicture}

\end{document}

AlexG
  • 54,894