.. 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. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_howto_plot_plot_animation.py: How to create Animations ======================== .. sectionauthor:: Florian Zill (Helmholtz Centre for Environmental Research GmbH - UFZ) 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 13-21 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import ogstools as ogs from ogstools import examples mesh_series = examples.load_meshseries_CT_2D_XDMF() .. GENERATED FROM PYTHON SOURCE LINES 22-32 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") You can also use a variable from the available presets instead of needing to create your own: :ref:`sphx_glr_auto_examples_howto_postprocessing_plot_variables.py` .. GENERATED FROM PYTHON SOURCE LINES 34-35 Let's use fixed scale limits to prevent rescaling during the animation. .. GENERATED FROM PYTHON SOURCE LINES 37-41 .. code-block:: Python ogs.plot.setup.vmin = 0 ogs.plot.setup.vmax = 100 ogs.plot.setup.dpi = 50 .. GENERATED FROM PYTHON SOURCE LINES 42-46 You can choose which timesteps to render by passing either an int array corresponding to the indices, or a float array corresponding to the timevalues to render. If a requested timevalue is not part of the timeseries it will be interpolated. In this case every second frame will be interpolated. .. GENERATED FROM PYTHON SOURCE LINES 48-52 .. code-block:: Python timevalues = np.linspace( mesh_series.timevalues()[0], mesh_series.timevalues()[-1], num=25 ) .. GENERATED FROM PYTHON SOURCE LINES 53-59 Now, let's animate the saturation solution. A timescale at the top indicates existing timesteps and the position of the current timevalue. Note that rendering many frames in conjunction with large meshes might take a really long time. We can pass two functions to `animate`: `mesh_func` which transforms the mesh and `plot_func` which can apply custom formatting and / or plotting. .. GENERATED FROM PYTHON SOURCE LINES 59-71 .. code-block:: Python def mesh_func(mesh: ogs.Mesh) -> ogs.Mesh: "Clip the left half of the mesh." return mesh.clip("-x", [0, 0, 0]) def plot_func(ax: plt.Axes, timevalue: float) -> None: "Add the time to the title." ax.set_title(f"{timevalue/(365.25*86400):.1f} yrs", loc="center") .. GENERATED FROM PYTHON SOURCE LINES 72-79 .. code-block:: Python anim = mesh_series.animate( ogs.variables.saturation, timevalues, mesh_func=mesh_func, plot_func=plot_func, ) .. container:: sphx-glr-animation .. raw:: html
.. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/25 [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 `