C应用程序的实时图表

ozg*_*gur 6 c graphics bar-chart livegraph

我有一个应用程序,它定期记录到主机系统,它可以在一个文件或只是一个控制台.我想用这些数据为我绘制统计图.我不确定我是否可以将实时图表用于我的应用程序.

如果这个工具是正确的,我可以举例说明将外部应用程序与实时图表集成吗?

这是livegraph链接 - > http://www.live-graph.org/download.html

moo*_*eep 6

我认为使用Python加matplotlib可以实现这一点.要实现这一点,实际上有多种方法:a)直接在C应用程序中集成Python解释器,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 plt\n"
    "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 %f\n", x, y);
  }

  if (plot) uninitializePlotting();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

您可以像这样构建它:

$ gcc plot.c -I /usr/include/python2.7 -l python2.7 -o plot
Run Code Online (Sandbox Code Playgroud)

并运行它:

$ ./plot --plot-data
Run Code Online (Sandbox Code Playgroud)

然后它将运行一段时间将红点绘制到轴上.

当您选择不直接绘制数据但将其打印到stdout您可以通过外部程序(例如,名为Python脚本plot.py)进行绘图时,该程序从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()
Run Code Online (Sandbox Code Playgroud)

我在我的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()
Run Code Online (Sandbox Code Playgroud)


Bor*_*ort 1

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

使用 LiveGraph 我必须说它的用途相当有限。我仍然会坚持使用带有 matplotlib 的 python 脚本,因为你可以更好地控制绘制的方式和内容。

#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 number\n");

        for(;;){
                rndnum = gsl_ran_gaussian(r, 1); 
                fprintf(f,"%f;%f\n", (double)t, rndnum);
                sleep(1);
                fflush(f);
                t = time(NULL);
        }   

        gsl_rng_free(r);
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译用

gcc -Wall main.c  `gsl-config --cflags --libs`
Run Code Online (Sandbox Code Playgroud)