C应用程序的实时图表

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

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

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

我认为使用Python加matplotlib可以实现这一点。 要实现这一点,实际上有多种方法:a)直接在C应用程序中集成Python解释器 ,b)将数据打印到stdout并将其传递给执行实际绘图的简单python脚本。 在下文中,我将描述两种方法。

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

 #include  #include  #include  #include  #include  #define CMD_BUF_SIZE 256 void initializePlotting() { Py_Initialize(); // load matplotlib for plotting PyRun_SimpleString("from matplotlib import pyplot as pp"); PyRun_SimpleString("pp.ion()"); // use pp.draw() instead of pp.show() } void uninitializePlotting() { Py_Finalize(); } void plotPoint2d(double x, double y) { // this buffer will be used later to handle the commands to python static char command[CMD_BUF_SIZE]; snprintf(command, CMD_BUF_SIZE, "pp.plot([%f],[%f],'r.')\npp.draw()", x, y); PyRun_SimpleString(command); } 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 < 1000; i++) { double x = myRandom(), y = myRandom(); if (plot) plotPoint2d(x,y); else printf("%f %f\n", 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 | python plot.pyplot.py类似于:

 from matplotlib import pyplot as pp pp.ion() 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 pp.plot([x],[y],'r.') pp.draw() 

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

编辑

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

 from matplotlib import pyplot as pp pp.ion() values = list() while True: data = [float(x) for x in raw_input().split()] values.append(data[0]) pp.clf() pp.hist([values]) pp.draw() 

那么,您只需要以给定的livegraph格式编写数据并设置livegraph以绘制您想要的内容。 如果写了小C的例子,它产生随机数并将它们与每秒的时间一起转储。 接下来,您只需将livegraph程序附加到该文件即可。 而已。

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

 #include  #include  #include  #include  #include  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; } 

编译

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