How to use Fortran + gnuplot to plot in real time in just one window?

I explored this recently when taking a course where my instructor would use gnuplot with reread. It works quite well, but another alternative is to use pyplot-fortran which calls Python’s Matplotlib from Fortran. Here’s an example code snippet that I constructed at the time. You can combine it with an image reader that would update automatically on change of the image to do what you want (Visual Studio Code for example).

program main
  use pyplot_module
  implicit none

  real :: x(100), y(100)
  type(pyplot) :: plt

  call plt%initialize()
  do
    call random_number(x)
    call random_number(y)
    call plt%add_plot(x, y, label="", linestyle=".")
    call plt%savefig("test.png")
!    call plt%destroy()
    call sleep(1)
  end do
end program main
2 Likes