I have a numerical simulation that I run with a short python code. I would like to know if it's possible to use tikzplotlib to produce a .tex file including the whole simulation (and not just one picture). I asked the question on SO. For the moment my sole idea is to use tikzplotlib to generate all the pictures of the animation, and try to find a solution in latex directly to produce an animation.
This means that I have a long list of .tex files of the form below, and I would like to produce an animation using latex. All the files have the same structure, they only differ in the values of the table. Is it possible to combine animate and a loop of \input commands or something like that ? A precision : since it's a simulation, I have to treat a rather large list of files (say a few thousands).
% This file was created with tikzplotlib v0.10.1.
\begin{tikzpicture}
\definecolor{darkgray176}{RGB}{176,176,176}
\definecolor{darkorange25512714}{RGB}{255,127,14}
\definecolor{steelblue31119180}{RGB}{31,119,180}
\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={darkgray176},
xmajorgrids,
xmin=-0.45, xmax=9.45,
xtick style={color=black},
y grid style={darkgray176},
ymajorgrids,
ymin=-0.15, ymax=3.15,
ytick style={color=black}
]
\addplot [semithick, steelblue31119180]
table {%
0 2.20822381790091
1 2.20822381790091
2 2.20822381790091
3 2.20822381790091
4 2.20822381790091
5 0
6 0
7 0
8 0
9 0
};
\addplot [semithick, darkorange25512714]
table {%
0 0
1 0
2 0
3 0
4 0
5 2.20822381790091
6 2.20822381790091
7 2.20822381790091
8 2.20822381790091
9 2.20822381790091
};
\draw (axis cs:0.05,0.9) node[
scale=0.5,
anchor=base west,
text=black,
rotate=0.0
]{time = 0.1971943887775551};
\end{axis}
\end{tikzpicture}
EDIT : Here is in an example of python code generating the tikz code above (only 500 frames here, I would already be happy to be able to use animate for it !).
import numpy as np
from scipy.integrate import solve_ivp
from matplotlib import pyplot as plt
from matplotlib import animation
Sites
M = 10
sites = np.array(range(M))
Particles
N = 3
Right hand side
def lotka(t,x):
u = x[0:M]
v = x[M:]
dudt = u-uu-uv
dvdt = v-uv-vv
dxdt = np.concatenate((dudt,dvdt))
return dxdt
Initial conditions
u0 = np.zeros(M)
v0 = np.zeros(M)
Segregated
u0[0] = N
v0[-1] = N
if M%2 == 0:
for i in range(int(M/2)):
u0[i] = N
v0[M-1-i] = N
else:
for i in range(int(np.floor(M/2))-1):
u0[i] = N
v0[M-1-i] = N
x0 = np.concatenate((u0,v0))
Solving the equation
nt = 500
t = np.linspace(0,0.2,nt)
x = solve_ivp(lotka,[0,4],x0,t_eval=t)
Getting each species from the solution of solve_ivp
u = np.zeros((nt,M))
v = np.zeros((nt,M))
for i in range(nt):
u[i] = x.y.T[i][0:M]
v[i] = x.y.T[i][M:]
Animation
data = [u, v]
fig = plt.figure()
ax = plt.axes()
ax.grid()
lines = [ax.plot(sites,u[0])[0], ax.plot(sites,v[0])[0]]
time_template = 'time = % s'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
lines.append(time_text)
import tikzplotlib
def animate(i,lines,data):
lines[0].set_ydata(data[0][i])
lines[1].set_ydata(data[1][i])
lines[2].set_text(time_template % t[i])
tikzplotlib.save("mytikz" + str(i) + ".tex")
return lines
anim = animation.FuncAnimation(fig,
animate,
#frames=200,
fargs=(lines,data),
interval=1,
blit=True)
plt.show()
animate-based animation. The output file would become too big. I would rather try to render it into an MP4 video. As an intermediate file format you could produce a multipage PDF using thestandaloneclass with class optiontikz. – AlexG Oct 09 '22 at 11:13tikzpicturecode above? – AlexG Oct 09 '22 at 19:47