2015-03-02 63 views
1

我正在探索racket中的繪圖工具,我不得不說,迄今爲止我對它印象非常深刻。但是,我遇到了一個小小的困難。單色球拍中的散點圖

我試圖做一個散點圖,其中每個點都有自己的顏色(用於可視化分類算法)。但是,我不知道如何讓每個點都有自己的顏色。可能嗎?

這裏是我到目前爲止已經試過:

(define (random-choice l) 
    (list-ref l (random (length l)))) 

(define (scatter data) 
    (let ([x-max (apply max (map first data))] 
     [x-min (apply min (map first data))] 
     [y-max (apply max (map second data))] 
     [y-min (apply min (map second data))] 
     [colors (build-list 20 
      ; here I am trying to create a list of desired colors 
      ; ,but an error tells me that only single elements are accepted 
      (λ _ (random-choice (list "red" "blue")))]) 
    (parameterize ([plot-new-window? #t] 
        [point-sym 'dot] 
        [point-size 20]) 
     (plot (points data 
      #:color colors) 
      #:x-max x-max 
      #:x-min x-min 
      #:y-max y-max 
      #:y-min y-min)))) 

大致相當於在R是:

c <- data.frame(x=rnorm(10),y=rnorm(10)) 
colors <- sample(c("red","blue"),10,replace=TRUE) 
plot(c$y,c$x,col=colors) 

順便說一句,我會歡迎任何(所有)評論我的代碼,例如簡化繪圖的邊界規範。

回答

2

plot函數接受渲染器列表(更一般地說,是一個tree of渲染器),並且可以爲每個渲染器提供其自己的顏色。

此外,自繪圖5.2以來,如果可能,plot函數將自動縮放繪圖的邊界以適合數據。顯然,這不適用於像(function sin)這樣的無限制,但對於points數據,如果不需要自定義邊界,則自動縮放就足夠了。

1

隨着@Alexis King的建議,我結束了這個簡單案例的兩個組。寫下後代。

(define-struct datum [point label]) 

(define (color-scatter data) 
    (let-values 
     ([(group-a group-b) (partition (λ (x) (= 1 (datum-label x))) data)]) 
    (plot (list 
      (points (map datum-point group-a) #:color "red") 
      (points (map datum-point group-b) #:color "blue")))))