假设我有一些数据(一个特定的向量)。 我可以使用Gnuplot按顺序逐个元素绘制它,使得它看起来好像是通过监视器追踪的真实信号吗?
我知道我可以使用Common Lisp将整个数据写入文本文件,然后使用gnuplot我可以批量格式绘制它。我需要的是,我希望在数据顺序出现时对我的情节说明一点。
数据可能会在循环内生成,因此您可以将x轴视为整数值离散时间轴。所以在循环中,如果数组的第一个元素生成为5,我想在我的绘图上加点(0,5)。然后,如果第二个元素生成为3,我想在我的绘图上添加另一个点(1,7)(保留旧数据点)。因此,当我遍历循环时,我会按顺序绘制数据。
我使用emacs和Common Lisp用于我的目的,我想将这些数据绘制在这些工具中。如果除了Gnuplot之外还有其他选项,我想听听它们。
如果我不能轻易实现这一点,那么如果我可以通过一些Common Lisp命令运行Gnuplot命令文件就会很酷。
编辑:
根据人们在此主题下提出的建议,我使用cgn
编写了一个使用ltk
的代码。
现在我在屏幕上预先指定的位置打开两个x11窗口,然后进入循环。在循环中,每次打开流并将数据(以20 Hz采样的0.25 Hz的正弦和余弦波)写入带有:if-exists :append
选项的文本文件trial.txt并关闭流。然后在每次迭代时,我使用gnuplot通过format-gnuplot
命令绘制整个数据。这段代码给了我两个预先指定的x和y范围的窗口,然后一个可以看到窗口中上述正弦波和余弦波的演变。
正如我之前所说,我没有强大的编程背景(我是一名电子工程师,不知何故使用常见的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)
非常感谢。
答案 0 :(得分:3)
答案 1 :(得分:2)
您可以创建gnuplot进程并将数据与绘图命令一起发送到它的stdin。我不确定如何在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")
通过这种异步过程,可以轻松编写内容,以便在新数据出现时以交互方式更新绘图。
答案 2 :(得分:2)
您可以查看orgplot模式,该模式将gnuplot绑定到emacs组织表。
答案 3 :(得分:2)
我可以使用eazy-gnuplot。请在此处查看:eazy-gnuplot examples。 github repo在这里:github repo。我没有更多时间在这里提供一个例子,抱歉。
答案 4 :(得分:1)
我对Gnuplot没有经验,快速搜索没有发现太多信息。但也许我可以提出一种方法。假设您输入了大量的内容,例如'(1 2 3 4 5)将是'((1)(1 2)(1 2 3)(1 2 3 4)(1 2 3 4 5)),那么你可以为每个生成一个绘图,并使用像lispbuilder-sdl这样的图形库在一个有时间延迟的窗口中显示它。 SDL有一个计时器,它可以很好地显示图像。