3

Trying to create an animated scatter plot that shows the cumulative data over time. I'd like to create an animation for this plot using the animate package:

enter image description here

Edits: For clarification, I'm hoping to plot each individual data point one at a time and play through the data so that at the end of the loop all the data is showing after having been plotted one at a time. I want to add one point at a given time, i.e. the first frame has only the first point of the table, the second one the first two, and so on.

Here is my attempt:

\documentclass[border=2pt,tikz]{standalone}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsbsy}


\usepackage{animate}
\usepackage{filecontents,pgfplots,pgfplotstable}
\pgfplotsset{compat=1.16}

\usetikzlibrary{plotmarks}
\usepgfplotslibrary{fillbetween}


% Blue
\definecolor{jhcobalta}{RGB}{0,59,92}
\definecolor{jhcobaltb}{RGB}{54,99,124}
\definecolor{jhcobaltc}{RGB}{104,138,156}
% Orange
\definecolor{jhcoppera}{RGB}{229,114,0}
\definecolor{jhcopperb}{RGB}{242,149,73}
\definecolor{jhcopperc}{RGB}{246,177,118}
% Red
\definecolor{jhcadmiuma}{RGB}{175,39,47}
\definecolor{jhcadmiumb}{RGB}{198,80,77}
\definecolor{jhcadmiumc}{RGB}{212,124,115}
% Grey
\definecolor{jhcarbona}{RGB}{136,139,141}
\definecolor{jhcarbonb}{RGB}{163,164,168}
\definecolor{jhcarbonc}{RGB}{185,185,185}

\definecolor{jhalgae}{RGB}{161,216,132}
\definecolor{jhaqua}{RGB}{119,197,213}
\definecolor{jhatomic}{RGB}{216,96,24}
\definecolor{jhamber}{RGB}{255,200,69}

\tikzstyle{arrow} = [very thick,->,>=stealth] 
\usepgfplotslibrary{units}
\usetikzlibrary{plotmarks}


\pgfplotsset{compat=1.15,
    colormap={mycolormap}{
        color(-1)=(jhcadmiuma) 
        color(0)=(jhamber) 
        color(1)=(jhcobalta)
    }
}

\begin{filecontents*}{data.dat}
Funding PM
8107.20 89
50850.00    88
400.00  88
400.00  88
600.00  87
4400.00 87
3308.26 87
3650.00 85
4400.00 85
5727.92 85
3216.00 85
2608.91 84
17237.88    84
203.59  84
6550.00 83
13895.00    83
2650.00 83
4686.33 82
800.00  82
808.80  82
1400.00 82
9570.00 81
1200.96 80
4362.60 80
24050.00    80
3219.69 80
7310.00 79
8768.60 79
6683.04 79
2525.00 79
10182.12    79
24259.77    79
800.00  78
2643.20 78
\end{filecontents*}


\pgfplotstableread{data.dat}\dataA


\pgfplotscreateplotcyclelist{my black white}{%
solid, every mark/.append style={fill=none}, mark=o\\%DS4
solid, every mark/.append style={fill=none},mark=+\\%1a
solid, every mark/.append style={fill=none}, mark=square\\%1
solid, every mark/.append style={fill=none},mark=x\\%DS2
solid, every mark/.append style={fill=none}, mark=triangle\\%DS3
solid, every text mark/.append style={fill=none}, mark options={scale=0.7, text mark=$s$}, mark=text\\%DS6
solid, every mark/.append style={fill=none},mark options={scale=0.5, text mark=$<$}, mark=text\\%DS7
% solid, every mark/.append style={fill=none},mark=Mercedes star\\%
% dashdotted, every mark/.append style={solid, fill=white},mark=otimes*\\%
% dasdotdotted, every mark/.append style={solid},mark=star\\%
% densely dashdotted,every mark/.append style={solid, fill=white},mark=diamond*\\%
}




\pgfplotsset{every axis/.style={scale only axis}}


\usepackage{siunitx}

\PassOptionsToPackage{per-mode=symbol,mode=text}{siunitx}

\pgfplotsset{every axis legend/.code={\let\addlegendentry\relax}} %%%if you don't want them

\begin{document}

\pgfplotstableread{data.dat}\dataA



%------------------------------------------------------------------------%
%------------------------------------------------------------------------%
%------------------------------------------------------------------------%
%------------------------------------------------------------------------%

\begin{animateinline}[controls]{10}
  \multiframe{5}{iIndex=0+1}{
\begin{tikzpicture}[scale=1]
\pgfplotsset{width=12cm, height=5cm},
\tikzset{every pin edge/.style={draw=red, ultra thick}}
\pgfplotsset{grid style={dashed,black!30}}
\begin{axis}[colorbar,
            colorbar=false,
            set layers,
minor tick num=0,
every y tick/.style={black},
every x tick/.style={black},
y tick label style={
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
    },
x tick label style={
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=2,
        /tikz/.cd
    },
legend pos=north east,   
legend cell align=left,
xmax=120000,
ymin=-100, ymax=125,
enlargelimits=true,
clip=false,
scaled ticks = true,
tick align = outside,
xtick scale label code/.code={},
tick align = outside,
xlabel={Funding  (\$)},
ylabel={Percent margin},
cycle list name= my black white,
axis x line=center, 
    axis y line=middle, 
    xticklabels={0,{\$25K},{\$50K},{\$75K},{\$100K},},
    xtick={0,25000,50000,75000,100000,125000}]
\foreach \i in {\dataA} {
\addplot+[jhcobalta,
    mark size=1.75, 
    mark=o,
    only marks,
        restrict y to domain=-80:100, 
        restrict x to domain=0:120000,
        scatter,
        fill=none] 
        table [header=true, 
        x = Funding, y=PM]{\dataA};
        }
        \end{axis}
    \end{tikzpicture}
  }
  \end{animateinline}


\end{document}
  • 1
    If you want the error to go away replace \documentclass[border=2pt,tikz]{standalone} by \documentclass[border=2pt]{standalone} \usepackage{tikz}. Other than that please explain the question a bit more. – Schrödinger's cat Oct 26 at 16:44
  • 1
    What I want to say is that I fail to see how the plots depend on iIndex. What precisely do you want to achieve? – Schrödinger's cat Oct 26 at 16:51
  • I'm hoping to plot each individual data point one at a time and play through the data so that at the end of the loop all the data is showing after having been plotted one at a time. I think my problem is setting up the for-each statement and tying that to the x-data "Funding." – Martin Clouthier Oct 26 at 16:53
  • 1
    So you want to add one point at a given time, i.e. the first frame has only the first point of the table, the second one the first two, and so on? – Schrödinger's cat Oct 26 at 16:56
  • @Schrödinger'scat that is exactly what I want. – Martin Clouthier Oct 26 at 16:58
5

OK, here is a proposal. I use a scatter plot and make the hidden marks infinitely small with

scatter/@pre marker code/.append
        style={/utils/exec=\pgfmathtruncatemacro{\myscale}{ifthenelse(\coordindex>\iIndex,0,1)},
    /tikz/scale=\myscale},

Obviously, one could also change the opacity or whatever to hide the marks. The crucial point is ifthenelse(\coordindex>\iIndex,0,1) which only adds the marks up to the one with an index no larger than the animation counter \iIndex.

This is the full code (in which I removed things that are not needed here in the edit).

\documentclass[border=2pt]{standalone}
\usepackage{animate}
%\usepackage[export]{animate}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.16}

% Blue
\definecolor{jhcobalta}{RGB}{0,59,92}
\definecolor{jhcobaltb}{RGB}{54,99,124}
\definecolor{jhcobaltc}{RGB}{104,138,156}
% Orange
\definecolor{jhcoppera}{RGB}{229,114,0}
\definecolor{jhcopperb}{RGB}{242,149,73}
\definecolor{jhcopperc}{RGB}{246,177,118}
% Red
\definecolor{jhcadmiuma}{RGB}{175,39,47}
\definecolor{jhcadmiumb}{RGB}{198,80,77}
\definecolor{jhcadmiumc}{RGB}{212,124,115}
% Grey
\definecolor{jhcarbona}{RGB}{136,139,141}
\definecolor{jhcarbonb}{RGB}{163,164,168}
\definecolor{jhcarbonc}{RGB}{185,185,185}

\definecolor{jhalgae}{RGB}{161,216,132}
\definecolor{jhaqua}{RGB}{119,197,213}
\definecolor{jhatomic}{RGB}{216,96,24}
\definecolor{jhamber}{RGB}{255,200,69}

%\usepgfplotslibrary{units}
%\usetikzlibrary{plotmarks}


\pgfplotsset{colormap={mycolormap}{
        color(-1)=(jhcadmiuma) 
        color(0)=(jhamber) 
        color(1)=(jhcobalta)
    }
}
\usepackage{filecontents}
\begin{filecontents*}{data.dat}
Funding PM
8107.20 89
50850.00    88
400.00  88
400.00  88
600.00  87
4400.00 87
3308.26 87
3650.00 85
4400.00 85
5727.92 85
3216.00 85
2608.91 84
17237.88    84
203.59  84
6550.00 83
13895.00    83
2650.00 83
4686.33 82
800.00  82
808.80  82
1400.00 82
9570.00 81
1200.96 80
4362.60 80
24050.00    80
3219.69 80
7310.00 79
8768.60 79
6683.04 79
2525.00 79
10182.12    79
24259.77    79
800.00  78
2643.20 78
\end{filecontents*}


\pgfplotstableread{data.dat}\dataA

\pgfplotstablegetrowsof{\dataA}%
\let\numberofrows=\pgfplotsretval
\typeout{\numberofrows}

\pgfplotscreateplotcyclelist{my black white}{%
solid, every mark/.append style={fill=none}, mark=o\\%DS4
solid, every mark/.append style={fill=none},mark=+\\%1a
solid, every mark/.append style={fill=none}, mark=square\\%1
solid, every mark/.append style={fill=none},mark=x\\%DS2
solid, every mark/.append style={fill=none}, mark=triangle\\%DS3
solid, every text mark/.append style={fill=none}, mark options={scale=0.7, text mark=$s$}, mark=text\\%DS6
solid, every mark/.append style={fill=none},mark options={scale=0.5, text mark=$<$}, mark=text\\%DS7
% solid, every mark/.append style={fill=none},mark=Mercedes star\\%
% dashdotted, every mark/.append style={solid, fill=white},mark=otimes*\\%
% dasdotdotted, every mark/.append style={solid},mark=star\\%
% densely dashdotted,every mark/.append style={solid, fill=white},mark=diamond*\\%
}


\pgfplotsset{every axis/.style={scale only axis}}

%\pgfplotsset{every axis legend/.code={\let\addlegendentry\relax}} %%%if you don't want them

\begin{document}

\pgfplotstableread{data.dat}\dataA


%------------------------------------------------------------------------%
%------------------------------------------------------------------------%
%------------------------------------------------------------------------%
%------------------------------------------------------------------------%

\begin{animateinline}[controls]{10}
  \multiframe{\numberofrows}{iIndex=0+1}{
\begin{tikzpicture}[scale=1]
\pgfplotsset{width=12cm, height=5cm},
%\tikzset{every pin edge/.style={draw=red, ultra thick}}
\pgfplotsset{grid style={dashed,black!30}}
\begin{axis}[minor tick num=0,
every y tick/.style={black},
every x tick/.style={black},
y tick label style={
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=0,
        /tikz/.cd
    },
x tick label style={
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=2,
        /tikz/.cd
    },
legend pos=north east,   
legend cell align=left,
xmax=120000,
ymin=-100, ymax=125,
enlargelimits=true,
clip=false,
scaled ticks = true,
tick align = outside,
xtick scale label code/.code={},
tick align = outside,
xlabel={Funding  (\$)},
ylabel={Percent margin},
cycle list name= my black white,
axis x line=center, 
    axis y line=middle, 
    xticklabels={0,{\$25K},{\$50K},{\$75K},{\$100K},},
    xtick={0,25000,50000,75000,100000,125000}]
\addplot+[jhcobalta,scatter, 
    scatter/@pre marker code/.append
        style={/utils/exec=\pgfmathtruncatemacro{\myscale}{ifthenelse(\coordindex>\iIndex,0,1)},
    /tikz/scale=\myscale},
    mark size=1.75, 
    mark=o,
    only marks,
        restrict y to domain=-80:100, 
        restrict x to domain=0:120000,
        scatter,
        fill=none] 
        table [header=true, 
        x = Funding, y=PM]{\dataA};
        \end{axis}
    \end{tikzpicture}
  }
  \end{animateinline}
\end{document}

And if you (see here) replace \usepackage{animate} by \usepackage[export]{animate}, you can convert the animation to a gif via

 convert -density 300 -delay 34 -loop 0 -alpha remove <file.pdf> <ani.gif>

enter image description here

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.