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.)

\usepackage{tikz, pgfplots}

%% automatic beamer numbering
  beamer externalizing/.style={%
    execute at end picture={%

\tikzset{every picture/.style={beamer externalizing}}
\tikzexternalize[only named=true]

\begin{frame}{gnuplot contour}
  \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};
  • Excuse the question, but why do you need to externalize and use beamer specifically for this, I have never used pgfplots with gnuplot.
    – BambOo
    May 15, 2020 at 18:39


Your Answer

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

Browse other questions tagged or ask your own question.