Question

I'm creating a plot of a robot's belief of its distance to a landmark. The x-axis is number of measurements, and the y-axis is distance to landmark, which should include error bars to indicate the confidence in this estimate.

I haven't been able to find an good way to add error bars to the plot based off a value for the variance. Currently I'm creating a box-plot at each measurement by generating sample data about the mean with my value for the variance. This is clearly not ideal, in that it is computationally inefficient and is an imprecise representation of the information I'm trying to display.

Any ideas for how to do this? Ideally it would be on an xy-plot, and it could be done without having to resort to JFreeChart commands.

Was it helpful?

Solution

I think I have something pretty close. First let's create some random data to graph:

(def y (for [i (range 20)] (rand-int 100)))
user> (11 14 41 33 25 71 52 34 83 90 80 35 81 63 94 69 97 92 4 91)

Now create a plot. You can use xy-plot but I like the look of scatter-plot better.

(def plot (scatter-plot (range 20) y))
(view plot)

That gives me the following plot

Random Data

Now we have to define a function that takes a point (x,y) and returns a vector of the lower and upper bounds of the error bar. I'll use a simplistic one that just calculates 5% above and below the y value.

(defn calc-error-bars [x y]
  (let [delta (* y 0.05)]
    [(- y delta) (+ y delta)]))

Now we just map that function over the set of data using the add-lines function like this...

(map #(add-lines plot [%1 %1] (calc-error-bars %1 %2)) (range 20) y)

And that gives us this plot:

Random Data with Error Bars

The main problem is that all the bars are different colors. I'm not sure if there is a way around this without using JFreeChart calls. Hopefully, someone will see this and tell me how to fix it. Anyway, that's pretty close.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top