clojure - Plotting nested vectors -
consider function outputs nested vector such as
[[[-0.6925523827697917 -0.4095089425269985] [-0.03856010899727634 0.8427233420960013] [-2.609986195686694e-13 -1.680032093051418e-12]] [[0.7203362514229046 -0.3494564274369062]]] in between single brackets, i.e. [-0.6925523827697917 -0.4095089425269985], numbers plotted in cartesian coordinates.
additionally there vectors within bracket, i.e.
[[0.7203362514229046 -0.3494564274369062]]
which denotes cluster.
i seeking plot points, vectors above, , draw line connecting points within cluster. so, points within cluster [[-0.6925523827697917 -0.4095089425269985] [-0.03856010899727634 0.8427233420960013] [-2.609986195686694e-13 -1.680032093051418e-12]] connected.
my first thought use incanter's xy-plot. part unsure how go indexed structure such vector point on plot. additionally, not sure how draw line connecting clustered points. example above should have 1 line (preferably smooth) through 3 points in first cluster, , no line through last cluster since there 1 point within cluster.
i'm not sure want, far got you, it's this:
(use '(incanter core charts interpolation)) (defn my-plot [[data [[cx cy]]]] (let [x (map first data) y (map second data) lbound (apply min x) rbound (apply max x) interp (interpolate data :cubic-hermite)] (-> (function-plot interp lbound rbound) (add-points x y) (add-points [cx] [cy]) view))) 
i'm using :cubic-hermite spline interpolation make line smooth, , i'm using add-points function add data points plot.
you'll find more interpolation examples here.
but 3 points isn't enough interpolation, should consider using linear interpolation instead:
(defn my-plot [[data [[cx cy]]]] (let [x (map first data) y (map second data)] (-> (xy-plot x y) (add-points x y) (add-points [cx] [cy]) view))) 
update:
let's have closer @ i'm doing here.
first, i'm using destructuring extract data points , cluster coordinates (i'm assuming have single cluster following data points):
(defn my-plot [[data [[cx cy]]]] then i'm breaking nested vector of [x y] pairs 2 vectors (one each dimension):
(let [x (map first data) y (map second data)] then i'm creating plot object , drawing line on using data points:
(-> (xy-plot x y) then i'm adding original data point (blue dots):
(add-points x y) and cluster (green dot):
(add-points [cx] [cy]) finally, i'm displaying resulting plot:
view))) in first example i'm using interpolation make line smoother:
lbound (apply min x) rbound (apply max x) interp (interpolate data :cubic-hermite)] (-> (function-plot interp lbound rbound) i'm using function-plot here because interp object function.
Comments
Post a Comment