源文地址

(September 2008)

For the last couple of years, I've been working on European Space Agency (ESA) projects - writing rather complex code generators. In the ESA project I am currently working on, I am also the technical lead; and I recently faced the need to (quickly) provide real-time plotting of streaming data. Being a firm believer in open-source, after a little Googling I found Gnuplot. From my (somewhat limited) viewpoint, Gnuplot appears to be the LaTEX equivalent in the world of graphs: amazing functionality that is also easily accessible. Equally important, Gnuplot follows the powerful paradigm that UNIX established: it comes with an easy to use scripting language, thus allowing its users to prescribe actions and "glue" Gnuplot together with other applications - and form powerful combinations.

To that end, I humbly submit a little creation of mine: a Perl script that spawns instances of Gnuplot and plots streaming data in real-time.


Plotting data in real-time

Interfacing over standard input

My coding experience has taught me to strive for minimal and complete interfaces: to that end, the script plots data that will arrive over the standard input, one sample per line. The samples are just numbers (integers / floating point numbers), and must be prefixed with the stream number ("0:", "1:", etc). Each plot window will also be configured to display a specific number of samples.

The resulting script is relatively simple - and easy to use:

bash ./driveGnuPlots.pl

Usage: ./driveGnuPlots.pl <options>
where options are (in order): NumberOfStreams How many streams to plot (windows)
Stream1_WindowSampleSize <Stream2...> This many window samples for each stream
Stream1_Title <Stream2_Title> ... Title used for each stream
(Optional) Stream1_geometry <...>. Sizes and positions in pixels The last parameters (the optionally provided geometries of the gnuplot windows)
are of the form:
WIDTHxHEIGHT+XOFF+YOFF

Note that the script uses the "autoscale" feature of GnuPlot, to automatically adapt to the incoming value ranges.

An example usage scenario: plotting sine and cosine

Let's say we want to see a sine and a cosine run side-by-side, in real-time. We also want to watch the cosine "zooming-in" by 10x (time-scale wise). The following code will print our test samples:

#!/usr/bin/perl -w
use strict; use Time::HiRes qw/sleep/; # First, set the standard output to auto-flush
select((select(STDOUT), $| = 1)[0]); # And loop 5000 times, printing values...
my $offset = 0.0;
while(1) {
print "0:".sin($offset)."\n";
print "1:".cos($offset)."\n";
$offset += 0.1;
if ($offset > 500) {
last;
}
sleep(0.02);
}

We'll use this code to test our plotting script: the data for two streams (sine and cosine) are printed in the expected format: one sample (one number) printed per line. To distinguish between the two streams, the sample is prefixed with "0:", "1:", etc. Notice that we explicitly set the autoflush flag for our standard output: we need the data output to be unbuffered, otherwise our plotting script will receive data in bursts (when the data are flushed from the producer), and the plots will "jerk" forward.

This is how we test the plotting script (assuming we saved the sample code above in sinuses.pl): <

bash$ ./sinuses.pl | ./driveGnuPlots.pl 2 50 500 "Sine" "Cosine"

To stop the plotting, use Ctrl-C on the terminal you spawned from.

The parameters we passed to driveGnuPlots.pl are:

  • 2 is the number of streams
  • The window for the first stream (sine) will be 50 samples wide
  • The window for the second stream (cosine) will be 500 samples wide (hence the different "zoom" factor)
  • The titles of the two streams follow

When executed, the script spawns one gnuplot per each stream, and displays the graphs in a clear, flicker-free manner. If you don't like the Gnuplot settings I used (e.g. the grid, or the colors, or...) feel free to change them: the setup code that defines the plotting parameters starts at line 82 of the script.

Executive summary: plotting streaming data is now as simple as selecting them out from your "producer" program (filtering its standard output through any means you wish: grep, sed, awk, etc), and outputing them, one number per line. Just remember to prefix with the stream number ("0:", "1:", etc, to allow for multiple streams), and make sure you flush your standard output, e.g.

For this kind of output:

    bash$ /path/to/programName
...(other stuff)
Measure: 7987.3
...(other stuff)
Measure: 8364.4
Measure: 8128.1
... You would do this: bash$ /path/to/programName | \
grep --line-buffered '^Measure:' | \
awk -F: '{printf("0:%f\n", $2); fflush();}' | \
driveGnuPlots.pl 1 50 "My data"

In the code above, grep filters out the lines that start with "Measure:", and awk selects the 2nd column ($2) and prefixes it with "0:" (since this is the 1st - and only, in this example - stream we will display). Notice that we used the proper options to force the standard output's flushing for both grep (--line-buffered) and awk (fflush() called).

Preparing for a demo

You don't want to move the GnuPlot windows after they are shown, do you? So you can just specify their placement, in "WIDTHxHEIGHT+XOFF+YOFF" format (in pixels):

bash$ ./sinus.pl | ./driveGnuPlots.pl 2 50 50 Sinus Cosinus 512x384+0+0 512x384+512+0

The provisioning of titles and GnuPlot window placement information, makes the script very well-suited for live demonstrations.

P.S. UNIX power in all its glory: it took me 30min to code this, and another 30 to debug it. Using pipes to spawned copies of gnuplots, we are able to do something that would require one or maybe two orders of magnitude more effort in any conventional programming language (yes, even accounting for custom graph libraries - you do have to learn their API and do your windows/interface handling...)

Update, November 30, 2009Andreas Bernauer has improved the script further, allowing multiple streams to be plotted in the same window. His work is here.
Update, December 20, 2009: Dima Kogan has done his own version, which detects the number of streams dynamically. He placed his code on GitHub.

Visualize real-time data streams with Gnuplot的更多相关文章

  1. FunDA(9)- Stream Source:reactive data streams

    上篇我们讨论了静态数据源(Static Source, snapshot).这种方式只能在预知数据规模有限的情况下使用,对于超大型的数据库表也可以说是不安全的资源使用方式.Slick3.x已经增加了支 ...

  2. NTFS格式下的Alternate Data Streams

    今天我写点NTFS的交换数据流以及其带来的安全问题(Alternate Data Stream/ADS) =============================================== ...

  3. Awesome Big Data List

    https://github.com/onurakpolat/awesome-bigdata A curated list of awesome big data frameworks, resour ...

  4. 翻译-In-Stream Big Data Processing 流式大数据处理

    相当长一段时间以来,大数据社区已经普遍认识到了批量数据处理的不足.很多应用都对实时查询和流式处理产生了迫切需求.最近几年,在这个理念的推动下,催生出了一系列解决方案,Twitter Storm,Yah ...

  5. Exploring the 7 Different Types of Data Stories

    Exploring the 7 Different Types of Data Stories What makes a story truly data-driven? For one, the n ...

  6. The difference between text mode and binary mode with file streams

    FIO14-C. Understand the difference between text mode and binary mode with file streams     Skip to e ...

  7. THE R QGRAPH PACKAGE: USING R TO VISUALIZE COMPLEX RELATIONSHIPS AMONG VARIABLES IN A LARGE DATASET, PART ONE

    The R qgraph Package: Using R to Visualize Complex Relationships Among Variables in a Large Dataset, ...

  8. Flink应用案例:How Trackunit leverages Flink to process real-time data from industrial IoT devices

    January 22, 2019Use Cases, Apache Flink Lasse Nedergaard     Recently there has been significant dis ...

  9. explore your hadoop data and get real-time results

    deep api integration makes getting value from your big data easy 深度api集成使你大数据訪问更加easy Elasticsearch ...

随机推荐

  1. asp.net.mvc 中form表单提交控制器的2种方法和控制器接收页面提交数据的4种方法

    MVC中表单form是怎样提交? 控制器Controller是怎样接收的? 1..cshtml 页面form提交 (1)普通方式的的提交

  2. smartstore-net

    记录一下,抽空下载源码了研究下

  3. Java8 如何进行stream reduce,collection操作

    Java8 如何进行stream reduce,collection操作 2014-07-16 16:42 佚名 oschina 字号:T | T 在java8 JDK包含许多聚合操作(如平均值,总和 ...

  4. SharpGL学习笔记(十六) 多重纹理映射

    多重纹理就把多张贴图隔和在一起.比如下面示例中,一个表现砖墙的纹理,配合一个表现聚光灯效果的灰度图,就形成了砖墙被一个聚光灯照亮的效果,这便是所谓的光照贴图技术. 多重纹理只在OpenGL扩展库中才提 ...

  5. 【GOF23设计模式】原型模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_原型模式.prototype.浅复制.深复制.Cloneable接口  浅复制 package com.test.prot ...

  6. mysql实时同步到mssql的解决方案

    数据库在应用程序中是必不可少的部分,mysql是开源的,所以很多人它,mssql是微软的,用在windows平台上是非常方便的,所以也有很多人用它.现在问题来了,如何将这两个数据库同步,即数据内容保持 ...

  7. Python基础(5)--字典

    字典由多个键及与其对应的值构成的对组成(把键值对成为项),每个键和它的值之间用冒号(:)隔开,项之间用逗号(,)隔开,而整个字典由一对大括号括起来.空字典由两个大括号组成:{} 本文地址:http:/ ...

  8. const,static,extern简介(重要)

    一.const与宏的区别(面试题): const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 编译时刻:宏是预编译(编译之前处理),const是编译 ...

  9. Cocos2d-x 基础元素

    看过本章,然后实践之后,应该会掌握以下的认识: 1.Cocos2d-x引擎的基本运转过程 2.Cocos2d-x引擎的一些初始设置 3.对导演及图层及现实对象的认识 4.如何定义自己的显示对象 *:f ...

  10. genymotion虚拟机启动失败

    错误提示如下: Make sure that you have installed it correctly before starting Genymotion. 解决方法(重启VirtualBox ...