plotting multiple data sets on one set of axes

42 views
Skip to first unread message

greadey

unread,
Apr 9, 2020, 2:35:04 AM4/9/20
to Racket Users
Hi All,

Can anyone give me some pointers on plotting multiple data sets on one set of axes.  Plotting two or more data sets on one set of axes is easy if you know in advance what the data is called;

(plot (list
      (lines set1)
      (points set2))
     #:x-label "x" #:y-label "y")

however I have a list of data sets generated from reading a bunch of files in a directory and I wish to loop through the list and add each data-set to an existing set of axes.

Iterating through the data and plotting each set results in multiple plot windows.

In summary I am looking for something similar to Matlab's "hold on" e.g.

;Pseudo Matlab code

figure()
for idx = 1 : numel(list-of (list-of vector-pairs))
    plot(list-of (list-of vector-pairs))(idx)
    hold on
end
hold off

I have got (plot-new-window? #t), however as far as I know this is just causing a plot to appear in a new window rather than directly in the repl.

Many thanks,

greadey.

Alex Harsanyi

unread,
Apr 11, 2020, 3:38:05 AM4/11/20
to Racket Users

The renderers  that you pass on to the `plot` function can be manipulated outside the call to plot, so you can read the datasets you need, construct a renderer for each one, group them in a list and pass them on to plot.  I am not familiar with Matlab, but based on your description, you can implement hold-on and hold-off as follows:

#lang racket
(require plot)

(define the-renderers '())

(define (hold-on renderer)
  (set! the-renderers (cons renderer the-renderers)))

(define (hold-off)
  (begin0
    (plot (reverse the-renderers))
    (set! the-renderers '
())))

;; add a renderer for SIN
(hold-on (function sin 0 10))

;; add a renderer for COS
(hold-on (function cos 0 10))

;; Show the plot
(hold-off)

Alternatively, if you want a separate plot for each function, you can implement hold-off as follows:

(define (hold-off-2)
 
(begin0
   
(for/list ([renderer (reverse the-renderers)])
     
(plot renderer))
   
(set! the-renderers '())))

Alex.

greadey

unread,
Apr 12, 2020, 7:33:34 PM4/12/20
to Racket Users
Thanks, Alex.   I managed to get that working a treat.
Reply all
Reply to author
Forward
0 new messages