尝试创建一个动画散点图,显示随时间推移的累积数据。我想使用 animate 包为该图创建一个动画:
编辑:为了澄清起见,我希望一次绘制一个单独的数据点并播放数据,以便在循环结束时,所有数据在一次绘制后都会显示出来。我想在给定时间添加一个点,即第一帧只有表格的第一个点,第二帧有前两个点,依此类推。
这是我的尝试:
\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
好的,这是一个建议。我使用散点图,并使用
scatter/@pre marker code/.append
style={/utils/exec=\pgfmathtruncatemacro{\myscale}{ifthenelse(\coordindex>\iIndex,0,1)},
/tikz/scale=\myscale},
显然,也可以改变不透明度或其他任何方式来隐藏标记。关键在于ifthenelse(\coordindex>\iIndex,0,1)
只添加索引不大于动画计数器的标记\iIndex
。
这是完整的代码(其中我删除了编辑中不需要的东西)。
\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}
如果你(见这里) 替换\usepackage{animate}
为\usepackage[export]{animate}
,您可以通过以下方式将动画转换为 gif
convert -density 300 -delay 34 -loop 0 -alpha remove <file.pdf> <ani.gif>