问题
Hi I am currently plotting data from a file through a pipe in c++. I have written a function that reads in the text file, processes the information, plots the data in the text file and plots labels and arrows as needed.
Now I need a way to plot smoothly in a loop, so that a different text file is read and plotted every iteration without the Gnuplot window flickering.
For example:
for (unsigned int i = 0; i< 10; i++){
Processing_Plotting(i, gp);
}
This is the code i have in the main program which will plot the text file indicated by 'i'. eg: laserData1, laserData2 etc..
Currently the plot flickers and is unreadable due to the speed that it is called. I have looked for a way to plot data in real time with Gnuplot but haven't had much luck, any help would be appreciated. Thanks
A picture of the sort of thing I am plotting: http://imgur.com/3eTpMaB
回答1:
There is an example of an animation that comes with the gnuplot-iostream library. It doesn't flicker, at least not on my machine (Linux). Disclaimer: I'm the author of that library, so that is a shameless plug.
Your code snippet doesn't show how you actually interface with gnuplot, however there are three things you must be sure to do: 1) don't close and then reopen the pipe after each frame, 2) call fflush
on the pipe after every frame, and 3) add a delay between frames. To delay on Linux call ::usleep(microseconds)
(and include unistd.h
), on Windows call ::Sleep(milliseconds)
and include windows.h
.
The thing about piping animation data to gnuplot is that you don't know if it is keeping up. So the flush and the sleep are vital. But I have built an oscilloscope out of gnuplot so I know it can work well.
来源:https://stackoverflow.com/questions/18101371/gnuplot-c-plotting-in-real-time-from-txt-files