3

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}
  • add hide axis option to the axis – percusse Feb 28 '16 at 17:05
  • I was just typing the same. Add [hide axis] to the axis environment. – Douwe66 Feb 28 '16 at 17:06
  • @percusse The new graph cannot be overlaid over the old one since the alignment and the bounding box change (from 78 mm × 62 mm to 69 × 57 mm). – AlexG Feb 28 '16 at 17:14
  • @AlexG Then probably I didn't understand the question. – percusse Feb 28 '16 at 17:16
2

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

  • 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 6:40

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.