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