现场图的C应用程序 [英] Live graph for a C application

查看:190
本文介绍了现场图的C应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有周期性登录到主机系统也可以是一个文件或只是一个控制台的应用程序。我想利用这些数据来绘制统计图给我。我不知道我是否可以使用实况图我的应用程序。

I have an application which logs periodically on to a host system it could be on a file or just a console. I would like to use this data to plot a statistical graph for me. I am not sure if I can use the live graph for my application.

如果这个工具是正确的,我可能对外部应用程序的实时图形整合的例子吗?

If this tool is the right one, may I have an example on integrating the external application with the live graph?

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

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

推荐答案

我认为这是可以实现使用Python最简单的加 matplotlib 。要做到这一点其实有多种方式:a)将在 Python的国米preTER 直接在C应用程序,b)数据到标准输出和管道这打印到一个简单的Python脚本,执行实际的绘图。下面我将描述这两种方法。

I think this can be achieved easiest using Python plus matplotlib. To achieve this there are actually multiple ways: a) integrating the Python Interpreter directly in your C application, b) printing the data to stdout and piping this to a simple python script that does the actual plotting. In the following I will describe both approaches.

我们有以下的C应用程序(例如 plot.c )。它使用了Python间preTER与matplotlib的绘图功能的接口。该应用程序能够直接绘制数据(当被称为像 ./情节--plot数据)和数据打印到标准输出(当与其他任何参数设置调用)。

We have the following C application (e.g. plot.c). It uses the Python interpreter to interface with matplotlib's plotting functionality. The application is able to plot the data directly (when called like ./plot --plot-data) and to print the data to stdout (when called with any other argument set).

#include <Python.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#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

然后,它会在一段时间绘制红色的点到轴上运行。

Then it will run for some time plotting red dots onto an axis.

当您选择不将数据直接绘制而是将其打印到标准输出则可以通过外部程序做绘图(如一个名为Python脚本 plot.py ),这需要从标准输入,即一个管道,并绘制它得到的数据。
为了实现这一目标调用程序像 ./情节|蟒蛇plot.py plot.py 是相似的:

When you choose not to plot the data directly but to print it to the stdout you may do the plotting by an external program (e.g. a Python script named plot.py) that takes input from stdin, i.e. a pipe, and plots the data it gets. To achieve this call the program like ./plot | python plot.py, with plot.py being similar to:

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.7 要安装的python-matplotlib

修改

我刚才看到的,你要绘制柱状图或这样的事情,这当然也是使用matplotlib可能的,例如直方图:

I have just seen, that you wanted to plot a bar plot or such thing, this of course is also possible using matplotlib, e.g. a histogram:

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()

这篇关于现场图的C应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆