.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/howto_plot/plot_animation.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_howto_plot_plot_animation.py: How to create Animations ======================== To demonstrate the creation of an animated plot we use a component transport example from the ogs benchmark gallery (https://www.opengeosys.org/docs/benchmarks/hydro-component/elder/). .. GENERATED FROM PYTHON SOURCE LINES 11-19 .. code-block:: Python import numpy as np import ogstools as ot from ogstools import examples ms = examples.load_meshseries_CT_2D_XDMF().scale(time=("s", "yrs")) saturation = ot.variables.saturation .. GENERATED FROM PYTHON SOURCE LINES 20-26 To read your own data as a mesh series you can do: .. code-block:: python from ogstools.meshlib import MeshSeries mesh_series = MeshSeries("filepath/filename_pvd_or_xdmf") .. GENERATED FROM PYTHON SOURCE LINES 28-31 You can choose which timesteps to render by either slicing the MeshSeries or by resampling it to new timesteps (this interpolates between existing timesteps). .. GENERATED FROM PYTHON SOURCE LINES 33-36 this would only use every second timesteps for animation ms = ms[::2] this uses equally spaced timesteps for a smooth animation .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python timevalues = np.linspace(ms.timevalues[0], ms.timevalues[-1], num=21) ms = ot.MeshSeries.resample(ms, timevalues) .. GENERATED FROM PYTHON SOURCE LINES 40-45 Now, let's animate the saturation solution. Note that rendering many frames in conjunction with large meshes might take a really long time. You need to setup a matplotlib figure first and a function, which is executed on each frame. This function has to take the individual values of the sequences passed as additional arguments, in this case the timevalues and the MeshSeries. .. GENERATED FROM PYTHON SOURCE LINES 48-49 clip to the right half .. GENERATED FROM PYTHON SOURCE LINES 49-64 .. code-block:: Python ms_r = ms.transform(lambda mesh: mesh.clip("-x", [-1, 0, 0])) # create initial figure with fixed colorbar fig = ot.plot.contourf(ms_r[0], saturation, vmin=0, vmax=100, dpi=50) fig.axes[0].set_title(f"{0} yrs", fontsize=32) def plot_contourf(timevalue: float, mesh: ot.Mesh) -> None: fig.axes[0].clear() ot.plot.contourf(mesh, saturation, ax=fig.axes[0], dpi=50) fig.axes[0].set_title(f"{timevalue:.1f} yrs", fontsize=32) anim = ot.plot.animate(fig, plot_contourf, ms_r.timevalues, ms_r) .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/20 [00:00 None: fig.axes[0].clear() ot.plot.line(mesh, saturation, ax=fig.axes[0]) fig.axes[0].set_ylim([0, 100]) fig.tight_layout() anim = ot.plot.animate(fig, plot_line, ms_x) .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/20 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_animation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_animation.zip `