【问题标题】:Live graph for a C applicationC 应用程序的实时图表
【发布时间】:2011-11-28 09:24:52
【问题描述】:

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

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

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

【问题讨论】:

  • 该工具的链接在哪里?
  • 您要绘制什么样的统计图? LiveGraph 似乎只支持 x/y 线图,但只要您以正确的格式写入文件,它应该能够显示您的图表。
  • 我有一个数据记录器文件。它的格式是 .txt 并且在一行中有数字。 (2,4 5,3 10,1 等)我想在一个可以工作的程序中使用这个文件
  • 最简单的方法是使用外部 python 脚本,使用 matplotlib 并在更新日志文件时进行重绘。如果您需要在 C 中执行此操作,gnuplot 也有一个 C 接口,它也可以很好地工作。只是另一个建议。

标签: c graphics bar-chart livegraph


【解决方案1】:

我认为这可以使用 Python 加上 matplotlib 最简单地实现。要实现这一点,实际上有多种方法:a) 将Python Interpreter 直接集成到您的 C 应用程序中,b) 将数据打印到标准输出并将其通过管道传输到一个简单的 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;
}

你可以这样构建它:

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

然后像这样运行它:

$ ./plot --plot-data

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

当您选择不直接绘制数据而是将其打印到stdout 时,您可以通过从stdin 获取输入的外部程序(例如名为plot.py 的Python 脚本)进行绘图,即管道,并绘制它获得的数据。 为了实现这个调用,像./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()

【讨论】:

    【解决方案2】:

    好吧,您只需要以给定的实时图格式写入数据并设置实时图以绘制您想要的内容。如果编写了一个小的 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;
    }
    

    编译

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 2011-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 2013-02-23
      相关资源
      最近更新 更多