1.2.3-SNAPSHOT Arrow_down_16x16

add-polygon

incanter.charts

  • (add-polygon chart coords & options)
Adds a polygon outline defined by a given coordinates. The last coordinate will
close with the first. If only two points are given, it will plot a line.

Arguments:
chart -- the chart to add the polygon to.
coords -- a list of coords (an n-by-2 matrix can also be used)


Examples:
(use '(incanter core stats charts))
(def x (range -3 3 0.01))
(def plot (xy-plot x (pdf-normal x)))
(view plot)

;; add polygon to the chart
(add-polygon plot [[-1.96 0] [1.96 0] [1.96 0.4] [-1.96 0.4]])
;; the coordinates can also be passed in a matrix
;; (def points (matrix [[-1.96 0] [1.96 0] [1.96 0.4] [-1.96 0.4]]))
;; (add-polygon plot points)
;; add a text annotation
(add-text plot -1.25 0.35 "95% Conf Interval")

;; PCA chart example
(use '(incanter core stats charts datasets))
;; load the iris dataset
(def iris (to-matrix (get-dataset :iris)))
;; run the pca
(def pca (principal-components (sel iris :cols (range 4))))
;; extract the first two principal components
(def pc1 (sel (:rotation pca) :cols 0))
(def pc2 (sel (:rotation pca) :cols 1))

;; project the first four dimension of the iris data onto the first
;; two principal components
(def x1 (mmult (sel iris :cols (range 4)) pc1))
(def x2 (mmult (sel iris :cols (range 4)) pc2))

;; now plot the transformed data, coloring each species a different color
(def plot (scatter-plot x1 x2
:group-by (sel iris :cols 4)
:x-label "PC1" :y-label "PC2" :title "Iris PCA"))

(view plot)
;; put box around the first group
(add-polygon plot [[-3.2 -6.3] [-2 -6.3] [-2 -3.78] [-3.2 -3.78]])
;; add some text annotations
(add-text plot -2.5 -6.5 "Setosa")
(add-text plot -5 -5.5 "Versicolor")
(add-text plot -8 -5.5 "Virginica")



0 Examples top

Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source incanter/charts.clj:2499 top

(defn add-polygon
" Adds a polygon outline defined by a given coordinates. The last coordinate will
  close with the first. If only two points are given, it will plot a line.

  Arguments:
    chart -- the chart to add the polygon to.
    coords -- a list of coords (an n-by-2 matrix can also be used)


  Examples:
    (use '(incanter core stats charts))
    (def x (range -3 3 0.01))
    (def plot (xy-plot x (pdf-normal x)))
    (view plot)

    ;; add polygon to the chart
    (add-polygon plot [[-1.96 0] [1.96 0] [1.96 0.4] [-1.96 0.4]])
    ;; the coordinates can also be passed in a matrix
    ;; (def points (matrix [[-1.96 0] [1.96 0] [1.96 0.4] [-1.96 0.4]]))
    ;; (add-polygon plot points)
    ;; add a text annotation
    (add-text plot -1.25 0.35 \"95% Conf Interval\")

    ;; PCA chart example
    (use '(incanter core stats charts datasets))
    ;; load the iris dataset
    (def iris (to-matrix (get-dataset :iris)))
    ;; run the pca
    (def pca (principal-components (sel iris :cols (range 4))))
    ;; extract the first two principal components
    (def pc1 (sel (:rotation pca) :cols 0))
    (def pc2 (sel (:rotation pca) :cols 1))

    ;; project the first four dimension of the iris data onto the first
    ;; two principal components
    (def x1 (mmult (sel iris :cols (range 4)) pc1))
    (def x2 (mmult (sel iris :cols (range 4)) pc2))

    ;; now plot the transformed data, coloring each species a different color
    (def plot (scatter-plot x1 x2
                            :group-by (sel iris :cols 4)
                            :x-label \"PC1\" :y-label \"PC2\" :title \"Iris PCA\"))

    (view plot)
    ;; put box around the first group
    (add-polygon plot [[-3.2 -6.3] [-2 -6.3] [-2 -3.78] [-3.2 -3.78]])
    ;; add some text annotations
    (add-text plot -2.5 -6.5 \"Setosa\")
    (add-text plot -5 -5.5 \"Versicolor\")
    (add-text plot -8 -5.5 \"Virginica\")



"
  ([chart coords & options]
    (let [opts (when options (apply assoc {} options))
          points (double-array (mapcat identity coords))
          anno (XYPolygonAnnotation. points)]
      (.addAnnotation (.getPlot chart) anno))))
Vars in incanter.charts/add-polygon: defn let
Used in 0 other vars

Comments top

No comments for add-polygon. Log in to add a comment.