1

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-u*u-u*v
    dvdt = v-u*v-v*v
    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()
5
  • A few thousand frames is definitely too much for an 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 the standalone class with class option tikz.
    – AlexG
    Commented Oct 9, 2022 at 11:13
  • Thanks. So the only way to use Tikz to modify the video would be to produce the frames by tikzplotlib, modify them, export (in png or pdf) and use animate ? I did not know of standalone, I'll check what it is.
    – xounamoun
    Commented Oct 9, 2022 at 12:01
  • Could you please add the Python script that produces the tikzpicture code above?
    – AlexG
    Commented Oct 9, 2022 at 19:47
  • Sure ! I added it in the post.
    – xounamoun
    Commented Oct 9, 2022 at 20:53
  • One option might be to create the frames using tikzpy for Python Commented Feb 1, 2023 at 20:09

1 Answer 1

2

The Python script outputs numbered files, each containing a single tikzpicture environment that represents one frame of the animation sequence. It is straightforward to assemble them into an animation:

\documentclass[margin=3pt]{standalone}

\usepackage{pgfplots}
\usepackage{animate}

\begin{document}

\begin{animateinline}[controls]{24}
\multiframe{500}{i=0+1}{
  \input{mytikz\i.tex}
}
\end{animateinline}

\end{document}
3
  • Thanks AlexG, unfortunately, as you suggested it before, for a large number of frames this is not adapted. I think I'll be able to adapt a solution based on this answer of yours (thanks by the way !!!) : tex.stackexchange.com/questions/235139/…
    – xounamoun
    Commented Oct 11, 2022 at 14:52
  • In case of thousands of frames, MP4 video is the only option then, I guess.
    – AlexG
    Commented Oct 11, 2022 at 15:19
  • I'll try to see if producing a large amounts of png files that I include with an animategraphics can work ...
    – xounamoun
    Commented Oct 11, 2022 at 15:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .