How do I show an animated plot? - c++

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

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().

Related

Real - time points plotting using opengl VC++

I need suggestion on how to plot points real-time. Using OpenGL
Right now what am doing is loading the desired data from a csv to an array and plotting the points from there. This is working fine.
What I intend to do is, load such multiple csv's one by one at regular intervals of time, so that i can create a animation kind of output. I can do this but once the program plots the point by entering glutMainLoop();, it never gets out without closing opengl window. I want to load the 1st csv, show it in OpenGL window, then load the next csv and show the new set of points and so on.
If its difficult to comprehend, just see the image below
Just consider the red and blue points.Consider them as not actually moving but being plotted from an external data with each new postions loaded from the csv file. Hope its clear
[...] once the program plots the point by entering glutMainLoop();, it never gets out without closing opengl window.
freeglut extends glut by glutLeaveMainLoop and glutMainLoopEvent.
e.g.:
bool condtion = true;
while (condtion)
{
glutMainLoopEvent(); // handle the main loop once
glutPostRedisplay(); // cause `display` to be called in `glutMainLoopEvent`
condtion = ...;
}
Another option would be to use glutIdleFunc to do additional stuff. So it is not necessary to leave the glut main loop at all..

How to display smooth video in FireMonkey (FMX, FM3)?

Has anyone figured out how to display smooth video (i.e. a series of bitmaps) in a FireMonkey application, HD or 3D? In VCL you could write to a canvas from a thread and this would work perfectly, but this does not work in FMX. To make things worse, the apparently only reliable way is to use TImage, and that seems to be updated from the main thread (open a menu and video freezes temporarily). All EMB examples I could find all either write to TImage from the main thread, or use Synchronize(). These limitations make FMX unusable for decent video display so I am looking for a hack or possibly bypass of FMX. I use XE5/C++ but welcome any suggestions. Target OS is both Windows 7+ & OS X. Thanks!
How about putting a TPaintbox on your form to hold the video. In the OnPaint method you simply draw the next frame to the paintbox canvas. Now put a TTimer on the form, set the interval to the frame rate required. In the OnTimer event for the timer just write paintbox1.repaint
This should give you regular frames no matter what else the program is doing.
For extra safety, you could increment a frame number in the OnTimer event. Now in the paintbox paint method you know which frame to paint. This means you won't jump frames if something else calls the paint method as well as the timer - you will just end up repainting the same frame for the extra call to OnPaint.
I use this for marching ants selections although I go one step further and use an overlaid canvas so I can draw independently to the selection and the underlying paintbox canvas to remove the need to repaint the main canvas when the selection changes. That requires calls to API but I guess you won't need it unless you are doing videos with a transparent colour.
Further research, including some talks with the Itinerant developer, has unfortunately made it clear that, due to concurrency restrictions, FM has been designed so that all GPU access goes through the main thread and therefore painting will always be limited. As a result I have decided FM is not suitable for my needs and I am re-evaluating my options.

setting position of a figure at current plot location in matplotlib

I am trying to record sound from mic using pyaudio and plotting it at the same time (in parallel with recording).
I have created a horizontal plot window, who's size is bigger than the main application window.
Code section:
self.fig, self.ax1 = plt.subplots()
rect = self.fig.patch
rect.set_facecolor('white')
#self.ax1.autoscale_view(True,True,True)
self.ax1.set_autoscale_on(True)
plt.subplots_adjust(left=0.0, bottom=0.0, top=0.95, right=10.0)
I am not able to move plot area to the current location of plotting or may be at the end of horizontal plot window. I also tried to use scrolled window. That also didn't work.
any idea?

Simulation making using MFC application

I am doing a simulation using MFC application (its a robot moving in a field) what happens is that the process behind calculates the position to quick where as drawing takes time so what I see ultimately is the robot at the end position no intermediate positions . But when I put AFXMessageBox then I can see all the position its been going through , Can you help me figure this out
Hina, What you need to do is move the complex calculation that calculates the position of the robot to a thread and keep the drawing of robot in your main thread. Then you need to communicate your current positions to the main thread and after the drawing invalidate your surface. This way you can see the positions updated frequently.
What happens when you display a message box is you are able to repaint the surface after the calculation.
You can fasten the drawing my using memory device context. In a nutshell you would do all the drawing on a bitmap in memory which will be fast. When it is all ready, you will print the final drawing on your display. This will be very fast and smooth.

How to make sure Qt Widget.repaint has finished running?

I am currently having a problem with Qt graphics view framework namely, I want to clear my QGraphicScene background color and then run a function to take a webcam picture. So far when I use QWidget.repaint the screen only got repaint after about 1 second and by then the camera function has been called and the image captured is always off. Here is how my code currently look like.
//Scene is a QGraphicScene
//View is a QGraphicView
//Camera is a camera object
Scene.setBackgroundBrush(Qt::Blue)
View.repaint()
Camera.Capture()
I have tried wrapping the repaint() call with another function and use signal and slot call but it still fail. I want to know if there is a way to pause the program until the screen has been refreshed.
A QGraphicsView has a bit more going on than most QWidget subclasses and I'm not familiar enough with it to say what is going on for sure but I might venture a guess that your problem is related to the fact that the scene is actually rendered onto the view port widget. Perhaps calling viewport->repaint() will give you the results you are looking for?
Also, unless you really need to be using the webcam in this scenario, you could call ::render() on your scene and pass it a QImage which you could save directly to a file.