我有一个应用程序定期登录到主机系统,它可能在文件上或只是控制台上。我想用这些数据为我绘制统计图。我不确定我是否可以在我的应用程序中使用实时图表。
如果这个工具是正确的,我可以举一个将外部应用程序与实时图集成的例子吗?
这是 livegraph 链接 --> http://www.live-graph.org/download.html
我有一个应用程序定期登录到主机系统,它可能在文件上或只是控制台上。我想用这些数据为我绘制统计图。我不确定我是否可以在我的应用程序中使用实时图表。
如果这个工具是正确的,我可以举一个将外部应用程序与实时图集成的例子吗?
这是 livegraph 链接 --> http://www.live-graph.org/download.html
我认为这可以使用 Python 加matplotlib最简单地实现。为了实现这一点,实际上有多种方法:a) 将Python 解释器直接集成到 C 应用程序中,b) 将数据打印到标准输出并将其通过管道传输到一个简单的 Python 脚本,该脚本执行实际绘图。下面我将介绍这两种方法。
我们有以下 C 应用程序(例如plot.c
)。它使用 Python 解释器与 matplotlib 的绘图功能交互。该应用程序能够直接绘制数据(当调用 like 时./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
您可以通过一个外部程序(例如名为 的 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()
我已经在我的 debian 机器上测试了这两种方法。它需要软件包python2.7
并被python-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 程序附加到文件中。而已。
玩 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`