Marine systems simulation
Live plotting using Python and matplotlib

Live plotting directly from SimObjects can be achieved by integrating with Python and matplotlib. This tutorial explains how.

This way of achieving live plotting depends on having Python (version >=3.6) and numpy and matplotlib installed on the computer running the simulation.

This tutorial will introduce an example project demonstrating setup of the plotting, before instruct how to include the plotting functionalities in your own SimObjects and tailor plots to your needs.

Compiling the example code (optional)

First, clone the project from code.sintef.no,

git clone ssh://git@git.code.sintef.no/fhsim/fhsim_pyplot.git

Build and compile the project:

conan build -s build_type=Release .

If you only plan to use the existing functionality, you can use a pre-built binary. You are encouraged to improve the library with pull-requests, fork it, or be inspired.

Adding live plotting to a SimObject

To include live-plotting functionalities in a SimObject you can add fhsim_pyplot as a requirement in your SimObject library. We assume your SimObject library has a conanfile.py.

  1. Add fhsim_pyplot/1.0.0@sintef/stable to its list of requirements.
  2. Add the following to your CMakeLists.txt:

    find_package(fhsim_pyplot MODULE REQUIRED)
    set(Python3_FIND_REGISTRY LAST)
    find_package(Python3 REQUIRED COMPONENTS Development.Embed NumPy)
    # ... add your simobject library fhsim_awesome
    target_compile_feature(fhsim_awesome PUBLIC cxx_std_17)
    target_link_libraries(fhsim_awesome PRIVATE
    fhsim_pyplot::pyplot
    Python3::Python
    Python3::NumPy)
  3. Include pyplot/pyplot.h in your SimObject source file. In pyplot there are two plotting-functions defined, namely plot_single() and plot3D_pos(). You are encouraged to contribute by adding more as described below.
  4. You may need to import pyplot.dll or libpyplot.so to your FhSim runtime directory.

Creating tailored plot-functions

Creating new plot functions is straightforward. Each plot function in pyplot/pyplot.cpp are linked to the matplotlibcpp.h-file. If you, as an example, want to create a function which plots two data-vectors, x1 and x2, in two subplots in the figure, you can add something similar to the following code. Observe that the syntax for creating a plot is quite similar to the syntax used by matplotlib in python.

void Plotx1x2(const std::vector<double> &time, const std::vector<double> &x1, const std::vector<double> &x2)
{
// Plotting
// Clear previous plot
matplotlibcpp::clf();
// Adding graph title
matplotlibcpp::title("Plot title");
matplotlibcpp::subplot(2, 1, 1);
matplotlibcpp::grid(true);
matplotlibcpp::named_plot("Data from x1", time, x1);
matplotlibcpp::legend();
matplotlibcpp::ylabel("[unit from x1]");
matplotlibcpp::subplot(2, 1, 2);
matplotlibcpp::grid(true);
matplotlibcpp::named_plot("Data from x2", time, x2);
matplotlibcpp::legend();
matplotlibcpp::ylabel(["Unit from x2"]);
matplotlibcpp::xlabel("Time [s]");
// Display plot continuously
matplotlibcpp::pause(0.01);
}