使用 Common Lisp 和 Gnuplot 从 emacs 顺序绘制数据

发布于 2025-01-05 10:42:42 字数 2210 浏览 2 评论 0原文

假设我有一些数据数组(具体来说是一个向量)。 我可以使用 Gnuplot 按顺序逐个元素地绘制它,这样它看起来就像是通过监视器跟踪的现实生活中的信号吗?

我知道我可以使用 Common Lisp 将整个数据写入文本文件,然后使用 gnuplot 我可以以批处理格式绘制它。我需要的是,当数据按顺序出现时,我想在我的绘图上放置一个点。

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

我正在使用 emacs 和 Common Lisp 来实现我的目的,并且我想在这些工具中绘制这些数据。如果除了 Gnuplot 之外还有其他选择,我想听听。

如果这不容易实现,那么如果我可以通过某些 Common Lisp 命令运行 Gnuplot 命令文件,那就太酷了。

编辑:

根据人们在此线程下给出的建议,我使用 cgn 编写了一个代码,该代码使用 ltk
现在,我在屏幕上预先指定的位置打开两个 x11 窗口,然后进入循环。每次我打开流并使用 :if-exists :append 选项将数据(以 20 Hz 采样的 0.25 Hz 正弦波和余弦波)写入文本文件 Trial.txt 并关闭时,都会在循环中溪流。然后,在每次迭代中,我通过 format-gnuplot 命令使用 gnuplot 绘制整个数据。这段代码为我提供了两个预先指定的 x 和 y 范围的窗口,然后可以在窗口中观察上述正弦波和余弦波的演变。
正如我之前所说,我没有很强的编程背景(我是一名电气工程师,不知何故最终使用了 common lisp),而且我很确定我的代码不是最佳的且不优雅。如果你们有一些进一步的建议、更正等,我真的很想听听。代码在这里:

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

非常感谢。

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?

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.

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.

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.

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

edit:

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)

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

愁杀 2025-01-12 10:42:42

cgn 是一个与 gnuplot 交互的通用 Lisp 解决方案,它使用 LTK

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

柠北森屋 2025-01-12 10:42:42

您可以创建 gnuplot 进程并将数据与绘图命令一起发送到它的标准输入。我不确定如何在 Common Lisp 中管理这样的过程,但你绝对可以在 Emacs 中做到这一点:

(setf *gnuplot-proc* (start-process "gnuplot" "*gnuplot-proc*" "gnuplot"))
;;; initiate plotting of data from stdin
(process-send-string *gnuplot-proc*
                     "plot \"-\" with lines\n")
;; send your data
(process-send-string *gnuplot-proc*
                     "5 -1\n4 -3.5\n3 9.5\n")
;; end of data, after this gnuplot would pop up interactive window
(process-send-string *gnuplot-proc* "e\n")

使用这样的异步过程,很容易编写一些东西,使其在新数据出现时交互式更新绘图。

You could create process to gnuplot and send data to it's stdin along with plotting commands. I'm not sure how to manage such process in Common Lisp but you definitely can do this in Emacs:

(setf *gnuplot-proc* (start-process "gnuplot" "*gnuplot-proc*" "gnuplot"))
;;; initiate plotting of data from stdin
(process-send-string *gnuplot-proc*
                     "plot \"-\" with lines\n")
;; send your data
(process-send-string *gnuplot-proc*
                     "5 -1\n4 -3.5\n3 9.5\n")
;; end of data, after this gnuplot would pop up interactive window
(process-send-string *gnuplot-proc* "e\n")

With such asynchronous process it's easy to write something to make it interactively update plot as new data comes along.

捎一片雪花 2025-01-12 10:42:42

您可能会看一下 orgplot 模式,它将 gnuplot 与 emacs org 表联系起来。

http://orgmode.org/worg/org-tutorials/org-plot.html

You might have a look at the orgplot mode, which ties gnuplot into emacs org tables.

http://orgmode.org/worg/org-tutorials/org-plot.html

青瓷清茶倾城歌 2025-01-12 10:42:42

你可以使用 eazy-gnuplot,我愿意。请参阅此处:eazy-gnuplot 示例。 github 存储库位于:github 存储库。抱歉,我没有更多时间在这里提供示例。

You could use eazy-gnuplot, I do. See it here: eazy-gnuplot examples. The github repo is here: github repo. I don't have more time to provide an example here, sorry.

陌上青苔 2025-01-12 10:42:42

我对 Gnuplot 没有经验,快速搜索并没有找到太多信息。但也许我可以提出一种方法。假设您对输入进行分块,例如 '(1 2 3 4 5) 将是 '((1) (1 2) (1 2 3) (1 2 3 4) (1 2 3 4 5)),然后您可以为每个生成一个绘图,并使用像 lispbuilder-sdl 将其显示在具有时间延迟的窗口中。 SDL 有一个计时器,它可以很好地显示图像。

I am not experienced with Gnuplot, and a quick search didn't turn up too much information. But perhaps i can propose an approach. Say you chunk your input, for example '(1 2 3 4 5) would be '((1) (1 2) (1 2 3) (1 2 3 4) (1 2 3 4 5)), you can then generate a plot for each, and use a graphics library like lispbuilder-sdl to display it in a window with a time delay. SDL has a timer and it can display images just fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文