1

I am using matplotlib.h to draw graphs with C++,

vector<double> Cx;
vector<double> Cy;
plt::plot(Cx,Cy,"*");
plt::show();

The problem is that I want to display the evolution of the two vectors with an animation like, so the function is here:

plt::show();

Is blocking the main thread and until the window is closed everything is blocked.

So is there anyway to change the plotted data without closing the Window and display to the user an animation

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
DJAMEL DAHMANE
  • 404
  • 4
  • 15
  • There is [an animation example](https://github.com/lava/matplotlib-cpp/blob/master/examples/animation.cpp). – Marek R Dec 09 '19 at 17:14

1 Answers1

1

I have only used matplotlib in python but from what you have there it seems similar. When to use cla(), clf() or close() for clearing a plot in matplotlib? <- using plt::clf() from here may be a good start. Then once the figure is cleared maybe have a time delay and replot.

plt.ion() # needed to say you want to reuse the same window
def display(some_list):
    plt.clf()
    plt.scatter(range(len(some_list)),some_list)
    plt.draw()

Somthing like this is used in python as an example note plt.draw() is being used to redraw on the same window rather than show().

Iain McL
  • 164
  • 1
  • 10
  • The problem is that i have to choose Blocking display or no blocking display , If I choose blocking display i can't do anything after that , and if i chose not blocking the main thread will exit and not showing anything – DJAMEL DAHMANE Dec 15 '19 at 09:56
  • 1
    That worked I just added plt::pause(delay) to prevent window closing – DJAMEL DAHMANE Dec 16 '19 at 10:17
  • Again I am not sure if it is the same in C++ but another method is using FuncAnimation https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.animation.FuncAnimation.html . This calls an update function repetedly to create an animation and can be used to make gifs etc. – Iain McL Dec 16 '19 at 10:42