Re: 2D Graphing package?

From: Andrew Dorrell <andrewd_at_ee.uts.edu.au>
Date: Wed, 26 Jun 1996 11:22:19 +1000 (EST)

>
> Is there a simple 2D graphing/plotting package? I just want STk to do
> some simple plotting of X-Y data with axes. Something like "(plot x
> y)", where x and y are sequences (lists or vectors) of data, which
> produces a plot of the data.

        This is probably not the answer you were after and I would
appreciate knowing about any responses you get which are more STk
based.

I had a similar need some time back and solved it by writing a simple
STk interface to the gnuplot package. It uses the process commands
available under STk to invoke and communicate with gnuplot. While the
interface I wrote was built to interface my own extended types to
gnuplot, it is fairly readily altered and so I have included it here
as an attachment. Please note that it is far from complete---it
serves my needs only.


The resulting interface is straightforward, eg:
(load "gnuplot")
(define data ...)
(define plot (make <gnuplot> :data-style 'lines))
(gnuplot plot `((,data "first data series)))
(eps plot "the_plot.eps") ;; save the plot to postscript


Cheers,
-- 
------------------------------------------------------------------------------
Mr Andrew Dorrell
School of Electrical Engineering                 *
University of Technology, Sydney                     *
PO Box 123                                     *
Broadway NSW 2007                                   .  
AUSTRALIA
                                                    *       /---\  Whoo?
Phone:   61 2 330 2395                                      (o o) /
Fax:     61 2 330 2435                                      ( : )  
email:   andrewd_at_ee.uts.edu.au                               ^ ^     
    OR   dorrell_at_ihf.uts.edu.au
------------------------------------------------------------------------------
begin gnuplot.stklos:
(define-class <gnuplot> ()
  ((stk-process)
   (data-file)
   (valid-terms)
   (valid-styles)
   (terminal	:allocation	:virtual
		:slot-ref	(lambda(o)
				  (let ((in-port  (process-input 
						   (slot-ref o 'stk-process)))
					(out-port (process-error
						   (slot-ref o 'stk-process))))
				    (flush out-port)
				    (format in-port "show term~A" #\newline)
				    (flush in-port)
				    (do ((s (string->symbol "")))
					((memq s (slot-ref o 'valid-terms)) s)
					(set! s (read out-port)))))
		:slot-set!	(lambda(o t)
				  (let ((in-port  (process-input 
						   (slot-ref o 'stk-process)))
					(out-port (process-error
						   (slot-ref o 'stk-process))))
				    (if (not (memq t (slot-ref o 'valid-terms)))
					(error "invalid term type"))
				    (format in-port "set term ~A~A"
					    (symbol->string t) #\newline)
				    (format in-port "replot~A" #\newline)
				    (flush in-port))))
   (data-style	:allocation	:virtual
		:slot-ref	(lambda(o)
				  (let ((in-port  (process-input 
						   (slot-ref o 'stk-process)))
					(out-port (process-error
						   (slot-ref o 'stk-process))))
				    (flush out-port)
				    (format in-port "show data style")
				    (newline in-port)
				    (flush in-port)
				    (do ((s (string->symbol "")))
					((memq s (slot-ref o 'valid-syles)) s)
					(set! s (read out-port)))))
		:slot-set!	(lambda(o s)
				  (let ((in-port  (process-input 
						   (slot-ref o 'stk-process)))
					(out-port (process-output
						   (slot-ref o 'stk-process))))
				    (if (not (memq s (slot-ref o 'valid-styles)))
					(error "invalid data style"))
				    (format in-port "set data style ~A~A"
					    (symbol->string s) #\newline)
				    (format in-port "replot~A" #\newline)
				    (flush in-port))))
))
(define-method initialize ((self <gnuplot>) initargs)
  (slot-set! self 'stk-process (run-process "gnuplot" 
					    :input :pipe
					    :output :pipe
					    :error :pipe))
  (slot-set! self 'valid-terms '(aifm 
				 atari
				 dumb 
				 epson 
				 gpic 
				 hpljii 
				 latex 
				 iris4d 
				 mf 
				 mif 
				 nec-cp6 
				 pbm 
				 pcl5 
				 postscript 
				 regis 
				 table
				 x11))
  (slot-set! self 'valid-styles '(lines 
				  points 
				  linespoints
				  dots 
				  steps
				  impulses 
				  errorbars 
				  boxes 
				  boxerrorbars))
  (slot-set! self 'data-file
	     (format #f ".plot-data~A"
		     (number->string
		      (process-pid
		       (slot-ref self 'stk-process)))))
  (let ((data-style (get-keyword :data-style initargs 'lines))
	(term	    (get-keyword :terminal initargs 'x11)))
    (slot-set! self 'data-style data-style)
    (slot-set! self 'terminal term)))
;;
;; define a method for plotting
;; the plot-args is a list of things to plot where each thing takes the
;; form of a list wih at least one item --- the data to be ploted (an narray)
;; The optional second item is a string to use as a legend:
;;
;;	plot item format: (data title)
;;
(define-method gnuplot ((self <gnuplot>) plot-args)
  (let* ((port (process-input (slot-ref self 'stk-process)))
	 (plot-no 1)
	 (plot-command (case (narray-dimension (caar plot-args))
			     ((1) "plot ")
			     ((2) "splot ")))
	 (write-1d (lambda(data port)
		     (narray-map-elements
		      (lambda(x) 
			(format port "~A~A" x #\newline) 0)
		      data)))
	 (write-2d (lambda(data port)
		     (narray-map-blocks 
		      (lambda(row) 
			(write-1d row port)
			(newline port)
			0)
		      (list (car (narray-shape data)) 1)
		      (list (car (narray-shape data)) 1)
		      data)))
	 (compose-plot
	  (lambda(plot-item)
	    (let* ((tmp-file (format #f "~A~A" 
				     (slot-ref self 'data-file) plot-no))
		   (tmp-port (open-output-file tmp-file))
		   (plot-data (car plot-item))
		   (plot-legend (if (> (length plot-item) 1) (cadr plot-item)
				  (format #f "~A" plot-no))))
	      (case (narray-dimension plot-data)
		    ((1) (write-1d plot-data tmp-port))
		    ((2) (write-2d plot-data tmp-port)))
	      (close-output-port tmp-port)
	      (set! plot-no (+ 1 plot-no))
	      (set! plot-command 
		    (string-append plot-command
				   (format #f "'~A' title '~A' " 
					   tmp-file plot-legend)))))))
    (compose-plot (car plot-args))
    (for-each
     (lambda(plot-item)
       (set! plot-command 
	     (string-append plot-command
			    (format #f ", ")))
       (compose-plot plot-item))
     (cdr plot-args))
    (format port plot-command)
    (newline port)
    (flush port)
    (after 100 (lambda()
		 (system (format #f "rm ~A*" (slot-ref self 'data-file)))))))
(define-method eps ((self <gnuplot>) file)
  (let ((in-port (process-input (slot-ref self 'stk-process))))
    (format in-port "set terminal postscript eps~A" #\newline)
    (flush in-port)
    (format in-port "set output ~A~A~A~Areplot~A" 
	    #\" file #\" #\newline #\newline)
    (flush in-port)
    (format in-port "set terminal ~A~Areplot~A" 
	    'x11 #\newline #\newline)
    (flush  in-port)))
end gnuplot.stklos
Received on Thu Jun 27 1996 - 05:58:08 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST