如何将矩阵绘制为球拍中的图像?

时间:2017-04-22 22:47:05

标签: plot scheme racket

我想用球拍做matplotlib.pyplot.matshow类似的事情。我知道这是一个微不足道的问题,也许我只是愚蠢,但在阅读了Racket绘图文档后我没有成功。

将转换为圆形图像的示例矩阵:

#lang typed/racket

(require math/array)
(require plot)

(: sq (-> Integer Integer))
(define (sq [v : Integer])
  (* v v))

(: make-2d-matrix (-> Integer Integer (Array Boolean)))
(define (make-2d-matrix [s : Integer] [r : Integer])
  (let ([center : Integer (exact-round (/ s 2))])
    (let ([a (indexes-array ((inst vector Integer) s s))])
      (let ([b (inline-array-map (λ ([i : (Vectorof Index)])
                            (+
                             (sq (- (vector-ref i 0) center))
                             (sq (- (vector-ref i 1) center))))
                          a)])
        (array<= b (array (sq r)))
        ))))


(array-map (λ ([i : Boolean]) (if (eq? i #f) 0 1)) (make-2d-matrix 20 6))

有人能给我一个暗示吗?

3 个答案:

答案 0 :(得分:2)

完全不是一个愚蠢的问题。这是很难与一群python库程序员竞争的领域之一。以下是我在Racket中的表现:

#lang racket


(require 2htdp/image
         math/array)

;; a 10x10 array
(define a
  (build-array #(10 10)
               (λ (e)
                 (match e
                   [(vector x y)
                    (cond [(= x y) x]
                          [else 0])]))))

;; map a value to a color
(define (cmap v)
  (color (floor (* 255 (/ v 10)))
         0
         (floor (* 255 (- 1 (/ v 10))))))

(apply
 above
 (for/list ([y (in-range 10)])
  (apply
   beside
  (for/list ([x (in-range 10)])
    (rectangle 10 10 'solid (cmap (array-ref a (vector x y))))))))

答案 1 :(得分:2)

根据您的情况,您可能对flomaps感兴趣:

http://docs.racket-lang.org/images/flomap_title.html?q=flbitmap

答案 2 :(得分:1)

我不确定你想要绘制什么。 plot库是围绕绘图功能而设计的,但我不知道你要表达的功能。

以下是绘制矩阵的两种方法:

(plot (points (cast (array->vector* m) (Vectorof (Vectorof Real)))

(plot3d (points3d (cast (array->vector* m) (Vectorof (Vectorof Real)))

cast是必需的,因为array->vector*的类型不够具体。