.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_gallery/animation/plot_wave_animation.f90" .. 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_gallery_animation_plot_wave_animation.f90: Travelling wave, saved as an animated GIF ========================================= Animation in fplotlib is deliberately plain: draw a figure, call :f:subr:`add_frame` to keep it, clear the figure, and repeat. When the loop is done, :f:subr:`save_animation` encodes everything collected so far into a GIF -- the encoder is part of the library, so no external tool is involved. The picture below is the finished GIF, playing in the page. .. GENERATED FROM PYTHON SOURCE LINES 10-25 .. code-block:: Fortran program plot_wave_animation use fplotlib, only: add_frame, clf, dp, fill_between, grid, plot, save_animation, & title, xlabel, xlim, ylabel, ylim implicit none integer, parameter :: n = 240, n_frames = 40 real(dp), parameter :: pi = 3.14159265358979323846_dp real(dp), parameter :: k = 2.0_dp, decay = 0.15_dp real(dp) :: x(n), y(n), envelope(n), phase integer :: frame, i do i = 1, n x(i) = 10.0_dp*real(i - 1, dp)/real(n - 1, dp) end do envelope = exp(-decay*x) .. GENERATED FROM PYTHON SOURCE LINES 26-37 Each frame shows the damped travelling wave :math:`y(x) = e^{-\gamma x} \sin(k x - \varphi)`, with the phase :math:`\varphi` advancing from :math:`0` to :math:`2\pi` over the loop. One frame per phase step. The axis limits are set explicitly inside the loop: without them every frame would autoscale to its own data and the animation would jitter. Frames must all come out the same size, which they do as long as the figure size and DPI stay put -- so keep anything that changes the layout, such as a longer title, out of the loop. .. GENERATED FROM PYTHON SOURCE LINES 39-55 .. code-block:: Fortran :dedent: 1 do frame = 1, n_frames phase = 2.0_dp*pi*real(frame - 1, dp)/real(n_frames, dp) y = sin(k*x - phase)*envelope call clf() call plot(x, y, "b-", lw=2.0_dp) call fill_between(x, y, color="b", alpha=0.2_dp) call xlim(0.0_dp, 10.0_dp) call ylim(-1.1_dp, 1.1_dp) call title("Travelling wave") call xlabel("x") call ylabel("displacement") call grid(.true.) call add_frame() end do .. GENERATED FROM PYTHON SOURCE LINES 56-59 :f:subr:`save_animation` writes the GIF and resets the frame buffer, so one program can build several animations in turn. ``fps`` sets the playback rate; pass ``loop=.false.`` for an animation that plays only once. .. GENERATED FROM PYTHON SOURCE LINES 60-64 .. code-block:: Fortran call save_animation("wave.gif", fps=20.0_dp) print *, "wrote wave.gif from", n_frames, "frames" end program plot_wave_animation .. image-sg:: /auto_gallery/animation/images/sphx_glr_plot_wave_animation_001.gif :alt: plot wave animation :srcset: /auto_gallery/animation/images/sphx_glr_plot_wave_animation_001.gif :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none wrote wave.gif from 40 frames .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.878 seconds) .. _sphx_glr_download_auto_gallery_animation_plot_wave_animation.f90: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Fortran source code: plot_wave_animation.f90 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_wave_animation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_