I am trying to externalize pgfplots
, gnuplot
and contour gnuplot
in beamer
. Towards this end I am using the visible on=<>
facility of \usetikzlibrary{overlay-beamer-styles}
(cf. this answer by user Daniel) as well as the automatic numbering solution from user Loop Space.
Compiling contour plots can be expensive. I noticed that when compiling tikz
image with multiple overlays containing contour gnuplots
, the contour plot gets computed for every single overlay. This is really wasteful since in should only be necessary to compute it a single time, and then include it or not when creating the externalized graphic depending on the visibility status.
Consider the following MWE. When compiling it it can be noticed that 9 sets of contour plots get computed, when only 3 would be necessary. How can this issue be avoided? Obviously the problem gets amplified the more overlays are produced, even if just a few nodes get uncovered. How to fix this problem?
I suspect a "proper" way to solve this may be to generate intermediate images and than combine them in the following way:
for k in num_overlays:
S_k = collection of all elements visible on overlay k
T_k = all elements in S_k visible for the first time
create intermediate image I_k from T_k
create final image J_k by overlaying I_j for j<=k
include image J_k on slide k
However I have no idea how to implement this algorithm.
(Notice that in contrast to my other question this one is not about the contours getting computed for the wrong frames, but rather if can avoid that it gets computed multiple times within a single tikzpicture
.)
\documentclass{beamer}
\usepackage{tikz, pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{external}
\usetikzlibrary{overlay-beamer-styles}
%% automatic beamer numbering
%\url{https://tex.stackexchange.com/q/119428/86}
\makeatletter
\newcommand*{\overlaynumber}{\number\beamer@slideinframe}
\tikzset{
beamer externalizing/.style={%
execute at end picture={%
\tikzifexternalizing{%
\ifbeamer@anotherslide
\pgfexternalstorecommand{\string\global\string\beamer@anotherslidetrue}%
\fi
}{}%
}%
},
external/optimize=false
}
\let\orig@tikzsetnextfilename=\tikzsetnextfilename
\renewcommand\tikzsetnextfilename[1]{\orig@tikzsetnextfilename{#1-\overlaynumber}}
\makeatother
\tikzset{every picture/.style={beamer externalizing}}
\tikzexternalize[only named=true]
\begin{document}
\begin{frame}{gnuplot contour}
\tikzsetnextfilename{contour}
\begin{tikzpicture}
\begin{axis}[view={0}{90}, domain=-2:2]
\addplot3[visible on=<1-3>,contour gnuplot={draw color=red,number=20, labels=false}] {x};
\addplot3[visible on=<2-3>,contour gnuplot={draw color=blue,number=20, labels=false}] {y};
\addplot3[visible on=<3-3>,contour gnuplot={draw color=black,number=20, labels=false}] {x^2+y^2};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
beamer
specifically for this, I have never usedpgfplots
withgnuplot
.