Plotting data sequentially from emacs using Common Lisp and Gnuplot
Asked Answered
F

5

5

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.

Formica answered 15/2, 2012 at 17:48 Comment(6)
So you want an empty window, and then, point after point all your data gets displayed with a delay between displaying point n and point n+1,say a second, am i understanding your question right?Judge
exactly, 1 second or depending on the sampling rate of my data Ts sec.Formica
Unless I'm mistaken this has barely nothing to with Emacs, right? For alternative solution, you might consider R, as illustrated on this CV thread. If I can think of a proper solution in CL/gnuplot, I will post it.Arbe
@Arbe thanks for the reply. the data will be generated in lisp, I know that in R and matlab I can plot dynamically but the issue is that whether I can communicate lisp with any other available plotting tool. If possible, I would like to do all within lisp, but if not I will need to somehow generate the data in lisp and somehow plot it using R, matlab, gnuplot etc.Formica
@YBE The problem is to update gnuplot graphical device, not really the binding between CL and gnuplot; I've used cgn in the past, but I'm not aware of dynamic plotting facilities with CL, unless you want to switch to something like xlispstat or Arc (not Paul Graham's Arc); both are based on a subset of CLISP.Arbe
@Arbe I really don't have much programming background. I have used matlab in general before. Right now I need to use lisp and to me any easily implementable solution is fine. Even though I asked the question for gnuplot, any tool is ok.Formica
K
3

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

Koren answered 16/2, 2012 at 23:9 Comment(5)
will cgn work dynamically as I asked in the question? Thanks a lot in advance.Formica
Yes, definitely. You just need to start-gnuplot, and then you can interactively evaluate different cgn functionsKoren
I have used cgn for plotting yesterday. It works. However I am not sure, how I can achieve the goal of plotting the data dynamically in a equivalent manner to the MATLAB's drawnow function in gnuplot. Do you have any idea? Thanks in advance.Formica
I'm not sure about how exactly drawnow works, but as far as I understand the term dynamically, you can do exactly that with cgn. First you start-gnuplot, and then every plotting function you evaluate causes on-the-fly changes to the drawn graph.Koren
Your code looks OK to me. The only significant comment is that in CL there's a special macro (with-open-file (str path :direction :output :if-exists :append :if-does-not-exist :create)) ...) (see: lispworks.com/documentation/lw50/CLHS/Body/m_w_open.htm) that is usually used instead of explicitly calling open and close on a file.Koren
F
2

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.

Fina answered 15/2, 2012 at 21:5 Comment(0)
F
2

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

Flatworm answered 15/2, 2012 at 21:7 Comment(0)
P
2

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.

Pluto answered 30/3, 2018 at 18:51 Comment(2)
Why this one over awesome-cl/plotting or quickdocs.org/search?q=plot ? thanksIchabod
@Ehvince, I didn't know about the ones you reference. I'll probably look into them. But I came at eazy-gnuplot from a different angle. The author of said library does some pretty intricate plotting in his work (machine learning research) and the interface seems amenable to leveraging pretty freely the power of gnuplot. It worked great for me combined with the GnuPlot Cookbook book the author recommended using.Pluto
J
1

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.

Judge answered 15/2, 2012 at 18:57 Comment(1)
Thanks for the reply. Your method seems doable, although it would be really better for me if there is a more direct way.Formica

© 2022 - 2024 — McMap. All rights reserved.