C应用程序的实时图形



我有一个定期登录到主机系统的应用程序,它可以在文件上,也可以只是在控制台上。我想用这些数据为我画一张统计图。我不确定我是否可以在我的应用程序中使用实时图形。

如果这个工具是正确的,我可以有一个例子集成外部应用程序与实时图形?

this is livegraph link -> http://www.live-graph.org/download.html

我认为使用Python + matplotlib可以最容易地实现这一点。要实现这一点,实际上有多种方法:a)将Python解释器直接集成到您的C应用程序中,b)将数据打印到stdout并将其管道传输到一个简单的Python脚本,该脚本执行实际绘图。下面我将描述这两种方法。

我们有以下C应用程序(例如plot.c)。它使用Python解释器与matplotlib的绘图功能进行交互。应用程序能够直接绘制数据(当像./plot --plot-data一样调用时)并将数据打印到stdout(当使用任何其他参数集调用时)。

#include <Python.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
void initializePlotting() {
  Py_Initialize();
  // load matplotlib for plotting
  PyRun_SimpleString(
    "from matplotlib import pyplot as pltn"
    "plt.ion()n"
    "plt.show(block=False)n"
    );
}
void uninitializePlotting() {
  PyRun_SimpleString("plt.ioff()nplt.show()");
  Py_Finalize();
}
void plotPoint2d(double x, double y) {
#define CMD_BUF_SIZE 256
  static char command[CMD_BUF_SIZE];
  snprintf(command, CMD_BUF_SIZE, "plt.plot([%f],[%f],'r.')", x, y);
  PyRun_SimpleString(command);
  PyRun_SimpleString("plt.gcf().canvas.flush_events()");
}
double myRandom() {
  double sum = .0;
  int count = 1e4;
  int i;
  for (i = 0; i < count; i++)
    sum = sum + rand()/(double)RAND_MAX;
  sum = sum/count;
  return sum;
}
int main (int argc, const char** argv) {
  bool plot = false;
  if (argc == 2 && strcmp(argv[1], "--plot-data") == 0)
    plot = true;
  if (plot) initializePlotting();
  // generate and plot the data
  int i = 0;
  for (i = 0; i < 100; i++) {
    double x = myRandom(), y = myRandom();
    if (plot) plotPoint2d(x,y);
    else printf("%f %fn", x, y);
  }
  if (plot) uninitializePlotting();
  return 0;
}

你可以这样构建它:

$ gcc plot.c -I /usr/include/python2.7 -l python2.7 -o plot

然后像这样运行:

$ ./plot --plot-data

然后它将运行一段时间,在轴上绘制红点。

当您选择不直接绘制数据,而是将其打印到stdout时,您可以通过外部程序(例如名为plot.py的Python脚本)进行绘图,该程序从stdin(即管道)获取输入,并绘制其获得的数据。要实现这一点,调用类似./plot | python plot.py的程序,plot.py类似于:

from matplotlib import pyplot as plt
plt.ion()
plt.show(block=False)
while True:
  # read 2d data point from stdin
  data = [float(x) for x in raw_input().split()]
  assert len(data) == 2, "can only plot 2d data!"
  x,y = data
  # plot the data
  plt.plot([x],[y],'r.')
  plt.gcf().canvas.flush_events()

我在我的debian机器上测试了这两种方法。它需要安装python2.7python-matplotlib两个包。

编辑

我刚刚看到,你想绘制一个条形图或类似的东西,这当然也可以使用matplotlib,例如直方图:

from matplotlib import pyplot as plt
plt.ion()
plt.show(block=False)
values = list()
while True:
  data = [float(x) for x in raw_input().split()]
  values.append(data[0])
  plt.clf()
  plt.hist([values])
  plt.gcf().canvas.flush_events()

那么,您只需要以给定的livegraph格式编写数据并设置livegraph以绘制您想要的内容。我写了一个小的C示例,它生成随机数并将它们与每秒钟的时间一起转储。接下来,您只需将livegraph程序附加到文件中。就是这样。

我必须说它的使用是相当有限的。我仍然会坚持使用python脚本与matplotlib,因为你有更多的控制如何和绘制什么。
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
int main(int argc, char** argv)
{
        FILE *f; 
        gsl_rng *r = NULL;
        const gsl_rng_type *T; 
        int seed = 31456;   
        double rndnum;
        T = gsl_rng_ranlxs2;
        r = gsl_rng_alloc(T);
        gsl_rng_set(r, seed);
        time_t t;
        t = time(NULL);

        f = fopen("test.lgdat", "a");
        fprintf(f, "##;##n");
        fprintf(f,"@LiveGraph test file.n");
        fprintf(f,"Time;Dataset numbern");
        for(;;){
                rndnum = gsl_ran_gaussian(r, 1); 
                fprintf(f,"%f;%fn", (double)t, rndnum);
                sleep(1);
                fflush(f);
                t = time(NULL);
        }   
        gsl_rng_free(r);
        return 0;
}

编译

gcc -Wall main.c  `gsl-config --cflags --libs`

最新更新