我想画一张每秒变化的图。我使用以下代码,它会定期更改图形。但是每次迭代都不会保留先前迭代的要点。我该怎么做?每秒只有一分。但是我想用历史数据绘制图形。
FILE *pipe = popen("gnuplot -persist", "w");
// set axis ranges
fprintf(pipe,"set xrange [0:11]\n");
fprintf(pipe,"set yrange [0:11]\n");
int b = 5;int a;
for (a=0;a<11;a++) // 10 plots
{
fprintf(pipe,"plot '-' using 1:2 \n"); // so I want the first column to be x values, second column to be y
// 1 datapoints per plot
fprintf(pipe, "%d %d \n",a,b); // passing x,y data pairs one at a time to gnuplot
fprintf(pipe,"e \n"); // finally, e
fflush(pipe); // flush the pipe to update the plot
usleep(1000000);// wait a second before updating again
}
// close the pipe
fclose(pipe);
一些评论:
using 1:2
规格。for
循环形式应为for (a = 0; a < 10; a++)
。gnuplot中没有一种好的方法来添加到已经存在的行,因此将要绘制的值存储在数组中并在该数组上循环可能是有意义的:
#include <vector>
FILE *pipe = popen("gnuplot -persist", "w");
// set axis ranges
fprintf(pipe,"set xrange [0:11]\n");
fprintf(pipe,"set yrange [0:11]\n");
int b = 5;int a;
// to make 10 points
std::vector<int> x (10, 0.0); // x values
std::vector<int> y (10, 0.0); // y values
for (a=0;a<10;a++) // 10 plots
{
x[a] = a;
y[a] = // some function of a
fprintf(pipe,"plot '-'\n");
// 1 additional data point per plot
for (int ii = 0; ii <= a; ii++) {
fprintf(pipe, "%d %d\n", x[ii], y[ii]) // plot `a` points
}
fprintf(pipe,"e\n"); // finally, e
fflush(pipe); // flush the pipe to update the plot
usleep(1000000);// wait a second before updating again
}
// close the pipe
fclose(pipe);
当然,您可能希望避免对魔术数字进行硬编码(例如10),但这只是一个示例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句