使用Common Lisp和Gnuplot从emacs顺序绘制数据 [英] Plotting data sequentially from emacs using Common Lisp and Gnuplot

查看:85
本文介绍了使用Common Lisp和Gnuplot从emacs顺序绘制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些数据数组(具体来说是向量). 我可以使用Gnuplot逐个元素地对其进行绘制,以使其看起来好像是通过监视器跟踪的真实信号吗?

Assume that I have some array of data (a vector to be specific). Can I plot it element by element sequentially using Gnuplot such that it seems as if it is a real life signal that is being traced through a monitor?

我知道我可以使用Common Lisp将整个数据写入文本文件,然后使用gnuplot可以以批处理格式将其绘制.我需要的是在数据连续出现时想在图上标出一个点.

I know that I can write the whole data into a text file using Common Lisp, then using gnuplot I can plot it in a batch format. What I require is that I want to put a point on my plot as data comes sequentially.

数据可能会在循环内生成,因此您可以将x轴视为整数离散时间轴.因此,在循环中,如果将数组的第一个元素生成为5,则我想在绘图上将一个点设置为(0,5).然后,如果第二个元素生成为3,则我想在图上将另一个点放到(1,7)(保留旧数据点).因此,当我遍历循环时,我将按顺序绘制数据.

Data will probably be generated inside a loop, thus you may consider x-axis as the integer valued discrete-time axis. So in the loop if the first element of the array is generated as 5, I would like to put a point on my plot to (0,5). Then if the second element is generated as 3, I would like to put another point on my plot to (1,7) (preserving the old data point). So as I iterate through the loop, I plot the data sequentially.

出于我的目的,我正在使用emacs和Common Lisp,我想将这些数据绘制在这些工具中.除了Gnuplot之外,还有其他选择.

I am using emacs and Common Lisp for my purposes and I want to plot this data staying within these tools. If there are any other options other than Gnuplot, I would like to hear them.

如果这不容易实现,那很酷,如果我可以通过一些Common Lisp命令运行Gnuplot命令文件.

If this is not easily possible, it would be cool, if I can run a Gnuplot command file by some Common Lisp command.

根据人们在此线程下给出的建议,我使用cgn编写了一个代码,该代码使用了ltk.
现在,我在屏幕上预先指定的位置打开两个x11窗口,然后进入循环.每次在循环中打开一个流,然后使用:if-exists :append选项将数据(以20 Hz采样的0.25 Hz的正弦和余弦波)写入文本文件trial.txt,然后关闭流.然后在每次迭代中,我通过format-gnuplot命令使用gnuplot绘制整个数据.这段代码为我提供了两个预先指定的x和y范围的窗口,然后您可以在窗口中观察上述正弦波和余弦波的演变.
正如我之前所说的,我没有很强的编程背景(我是一位电气工程师,以某种方式结束了使用通用Lisp的工作),而且我很确定我的代码是非最优且毫无歧义的.如果你们还有其他建议,更正等,我真的很想听听他们的意见.代码在这里:

Following advices people gave under this thread, I wrote a code using cgn which uses ltk.
Right now I open two x11 windows at pre-specified positions on my screen and I enter the loop. In loop each time I open a stream and write the data (sine and cosine waves of 0.25 Hz sampled at 20 Hz) to the text file trial.txt with the :if-exists :append option and close the stream. Then at each iteration I plot the whole data using the gnuplot through the format-gnuplot command. This code gives me two windows of pre-specified x and y ranges and then one can watch the evolutions of aforementioned sine and cosine waves in the windows.
As I have stated before I don't have strong programming background (I am an electrical engineer who somehow ended using common lisp) and I am pretty sure that my code is non-optimal and unelegant. If you guys have some further advices, corrections etc. I would really like to hear them. The code is here:

(setf filename "trial.txt")
(setf path (make-pathname :name filename))
(setf str (open path :direction :output  :if-exists :supersede :if-does-not-exist :create))
(format str "~,4F ~,4F" -1 -1)
(close str)

;start gnuplot process
(start-gnuplot "/Applications/Gnuplot.app/Contents/Resources/bin/gnuplot")

;set 2 x11 windows with the following properties
(format-gnuplot "cd ~S" "Users/yberol/Desktop/lispbox/code")
(format-gnuplot "set terminal x11 0 position 0,0")
(format-gnuplot "set xrange [0:10]")
(format-gnuplot "set yrange [-1:1]")
(format-gnuplot "unset key")
(format-gnuplot "set grid")

(format-gnuplot "plot ~S using 1" filename)
(format-gnuplot "set terminal x11 1 position 800,0")
(format-gnuplot "plot ~S using 2" filename) 

;write data into text 
(loop :for i :from 0 :to 10 :by (/ 1 20) :do
   (setf str (open path :direction :output  :if-exists :append :if-does-not-exist :create))
   (format str "~,4F ~,4F ~,4F ~%" i (sin (* 2 pi (/ 5 20) i)) (cos (* 2 pi (/ 5 20) i)))
   (close str)
   (format-gnuplot "set terminal x11 0")
   (format-gnuplot "plot ~S using 1:2 with lines" filename)
   (format-gnuplot "set terminal x11 1")
   (format-gnuplot "plot ~S using 1:3 with lines" filename)
   (sleep 0.1))
(close-gnuplot)

非常感谢您.

推荐答案

cgn 是可正常使用的Lisp与gnuplot交互的解决方案,该解决方案使用 LTK

cgn is a working Common Lisp solution for interfacing with gnuplot, which uses LTK

这篇关于使用Common Lisp和Gnuplot从emacs顺序绘制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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