4

我正在寻找一种方法来可视化在用 clojure 编写的模拟中更新的 2d java 数组,就像我在 matplotlib 中使用 imshow 来可视化 numpy 数组一样。

最好的方法是什么?或者,我可以将数组保存到磁盘并在 matplotlib 中可视化。最好的方法是什么?


这是我基于此处的 Java 代码的尝试,但使 BufferedImage 非常慢。有没有办法加快速度?:

(import 
 '(java.awt Color Graphics Graphics2D Dimension GradientPaint BorderLayout)
 '(java.awt.image BufferedImage)
 '(javax.swing JPanel JFrame))

(def L 1024)

(def image (BufferedImage. (* L 1) (* L 1) (. BufferedImage TYPE_INT_RGB)))
(def g2 (. image createGraphics))

(defn get-color-map []
  (let [STEPS 100
        colormap (BufferedImage. STEPS 1 (BufferedImage/TYPE_INT_RGB))
        g (.createGraphics colormap)
        paint (GradientPaint. 0 0 (Color/RED) STEPS 0 (Color/GREEN))
        ]
    (doto g
      (.setPaint paint)
      (.fillRect 0 0 STEPS 1))
    colormap))

(defn get-color [x start finish colormap]
  (let [y (/ (- x start) (- finish start))
        STEPS 100]
    (Color. (.getRGB colormap (int (* y STEPS)) 0))))

(defn fill-image [^"[[D" arr ^Graphics2D g sideX sideY ^BufferedImage colormap]
  (dotimes [i (alength arr)]
    (dotimes [j (alength ^"[D" (aget arr 0))]
       (doto g
         (.setColor (get-color (aget ^"[[D" arr (int i) (int j)) -10.0 10.0 colormap))
         (.fillRect (int (* i sideX)) (int (* j sideY)) sideX sideY)))))


(def panel
     (doto (proxy [JPanel] []
             (paintComponent [g] (.drawImage g image 0 0 nil)))))

(def frame
     (doto (JFrame. "Heat Map")
       (.add panel BorderLayout/CENTER)
       (.pack)
       (.setLocationRelativeTo nil)
       (.setVisible true)))

这是使用 incanter 处理的尝试。它也很慢:

(let [sktch (sketch
             (setup []
                    (doto this
                      ;no-loop
                      (size 1024 1024)
                      (framerate 15)
                      smooth))

              ;; define the draw function
              (draw []
                    (def A (gaussian-matrix 1024 0 1))
                    (dotimes [i 1024]
                      (dotimes [j 1024]
                        (doto this
                          (stroke (int (abs (* (aget A i j) 255))))
                          (point i j))))))]

  (view sktch :size [1024 1024]))
4

2 回答 2

2

使用 Octave 的java 包将 Java 对象获取到 Octave 中,然后调用 Octave 的 imshow。

于 2010-09-28T19:46:44.983 回答
0

它不是一个完整的等价物,尽管您可能会使用Pretty Printer Library帮助您入门:

(pprint (for [x (range 10)] (range x)))         
(()
 (0)
 (0 1)
 (0 1 2)
 (0 1 2 3)
 (0 1 2 3 4)
 (0 1 2 3 4 5)
 (0 1 2 3 4 5 6)
 (0 1 2 3 4 5 6 7)
 (0 1 2 3 4 5 6 7 8))
零

这与 imshow 的规模不同(仅作为文本),所以我真的建议您在找到更漂亮的东西之前可能想要使用它。

于 2010-09-28T17:41:47.530 回答