Gnuplot - 每秒更新图形
Gnuplot - Update graph each second
我想画一个每秒变化的图表。我使用以下代码,它会定期更改图形。但是每次迭代都不会保留前一次迭代的点。我该怎么做?每秒只有一个点。但我想用历史数据绘制图表。
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);
几点评论:
- gnuplot 中的默认值是 x 数据来自第一列,y 数据来自第二列。您不需要
using 1:2
规范。 - 如果需要 10 个图,则
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 %dn", x[ii], y[ii]) // plot `a` points
}
fprintf(pipe,"en"); // 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),但这只是一个示例。
相关文章:
- 从C++本机插件更新Vector3数组
- QGraphicsPolygonItem在拖动时未更新QPolygonF坐标
- C++图形类指针混淆
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 如何在内核C++中使用1920x1080x16M图形或类似的16M颜色?(VGA)
- cmake更新缓存的变量
- 更新到莫哈韦后出现cmath错误
- OpenMP:并行更新数组总是需要减少数组吗
- 为什么我的变量没有更新,我的 LED 没有亮起?
- 这是实现图形的坏方法吗
- 指针没有更新它在void函数内部指向的值
- 如何在c++中获取要更新的值
- 有没有办法在更改与 HDC 关联的位图大小后更新图形对象?
- 从其他线程更新QT图形视图
- QCustomPlot/Widget不显示图形/更新
- Gnuplot - 每秒更新图形
- 如何在Qt小工具项目中更新图形视图(Qt Creator)
- 基于文本的图形未正确更新
- matlab编译器:更新数据到图形窗口
- 我如何确定为什么OpenGL入口点丢失后更新我的图形驱动程序