假设我有一些数据数组(具体来说是一个向量)。
我可以使用 Gnuplot 按顺序逐个元素地绘制它,这样它看起来就像是通过监视器跟踪的现实生活中的信号吗?
我知道我可以使用 Common Lisp 将整个数据写入文本文件,然后使用 gnuplot 我可以以批处理格式绘制它。我需要的是,当数据按顺序出现时,我想在我的绘图上放置一个点。
数据可能会在循环内生成,因此您可以将 x 轴视为整数值离散时间轴。因此,在循环中,如果数组的第一个元素生成为 5,我想在绘图上放置一个点到 (0,5)。然后,如果第二个元素生成为 3,我想将绘图上的另一个点放置到 (1,7)(保留旧数据点)。因此,当我迭代循环时,我会按顺序绘制数据。
我正在使用 emacs 和 Common Lisp 来实现我的目的,并且我想在这些工具中绘制这些数据。如果除了 Gnuplot 之外还有其他选择,我想听听。
如果这不容易实现,那么如果我可以通过某些 Common Lisp 命令运行 Gnuplot 命令文件,那就太酷了。
edit:
根据人们在该线程下给出的建议,我使用以下代码编写了代码cgn
它使用ltk
.
现在,我在屏幕上预先指定的位置打开两个 x11 窗口,然后进入循环。每次我打开一个流并将数据(以 20 Hz 采样的 0.25 Hz 的正弦波和余弦波)写入文本文件 Trial.txt 时,都会在循环中使用:if-exists :append
选项并关闭流。然后,在每次迭代时,我使用 gnuplot 绘制整个数据format-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)
非常感谢。