Plot with error bars

8 views
Skip to first unread message

Brendan Tracey

unread,
May 7, 2014, 4:58:56 PM5/7/14
to plotinum...@googlegroups.com
I have a list of means and standard deviations, and I would like to make a (log-log) error bar plot using them. This is doable, but I'm confused how to do so without using the plotutil functions to generate them for me. I see there is the YErrorBars type and the YErrorer interface, but I don't quite see how to use them. XY should be the location of the mean, and YErrorer should return the +/- 2 standard deviations for each mean?

Thanks.

Ethan Burns

unread,
May 7, 2014, 6:58:17 PM5/7/14
to Brendan Tracey, plotinum...@googlegroups.com
Hi Brendan,

Honestly, I'd have to look at the code myself, and I'm on a train at the moment. At the latest, I'll have a nice answer for you this weekend.

Also, once we figure it out, would you be able to make a simple example for the wiki? I imagine this is a common use case; it should be better documented.

Ethan


On Wednesday, May 7, 2014 4:58:57 PM, Brendan Tracey <tracey....@gmail.com> wrote:
I have a list of means and standard deviations, and I would like to make a (log-log) error bar plot using them. This is doable, but I'm confused how to do so without using the plotutil functions to generate them for me. I see there is the YErrorBars type and the YErrorer interface, but I don't quite see how to use them. XY should be the location of the mean, and YErrorer should return the +/- 2 standard deviations for each mean?

Thanks.

--
You received this message because you are subscribed to the Google Groups "plotinum-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plotinum-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brendan Tracey

unread,
May 7, 2014, 7:01:59 PM5/7/14
to Ethan Burns, plotinum...@googlegroups.com
Okay, thanks. I’ll try to look at the code more.

Here is the code I have. This produces Y error bars, but not where I think it should. Below is the output from the println statement in YError. I removed the log-log for the moment for simplicity.

type Eim struct {
MeanSquaredError float64
ErrorInMean      float64
}

type errorBarPlotter struct {
Locs []float64
Eims []Eim
}

func (e errorBarPlotter) Len() int {
return len(e.Eims)
}

func (e errorBarPlotter) XY(i int) (x, y float64) {
return e.Locs[i], e.Eims[i].MeanSquaredError
}

func (e errorBarPlotter) YError(i int) (float64, float64) {

plus2std := 2 * e.Eims[i].ErrorInMean
minus2std := -2 * e.Eims[i].ErrorInMean

bar1 := minus2std + e.Eims[i].MeanSquaredError
bar2 := plus2std + e.Eims[i].MeanSquaredError

fmt.Println(i, e.Eims[i].MeanSquaredError, bar1, bar2)

return bar1, bar2
}


0 1.5617167432025017e+06 1.187664112545156e+06 1.9357693738598474e+06
1 1.695196698805897e+06 1.1148426835788495e+06 2.2755507140329443e+06
2 1.2531841682256493e+06 930109.8740661875 1.576258462385111e+06
3 858163.9527657714 626460.9728924123 1.0898669326391304e+06
4 893163.7317070792 660608.2627222275 1.125719200691931e+06
5 546863.2267076651 392499.2763893133 701227.177026017
6 492155.1685031886 361566.8992305494 622743.4377758277
7 426622.13110903185 312433.1624552479 540811.0997628157

Ethan Burns

unread,
May 7, 2014, 8:17:45 PM5/7/14
to plotinum...@googlegroups.com

Forgot to CC the list.

---------- Forwarded message ----------
From: "Brendan Tracey" <tracey....@gmail.com>
Date: May 7, 2014 7:49 PM
Subject: Re: Plot with error bars
To: "Ethan Burns" <burns...@gmail.com>
Cc:

Okay, no rush. I’d love to submit a patch, but I’m pretty swamped in the near future, so I don’t think I’ll get to it before this weekend. If you are also feeling swamped, I think an example on the wiki like your code is sufficient. (though, preferably with different sized errors on each side to show that the first number is subtracted and the second added). The rest of it is pretty easy to follow.

Thanks again for the package!

On May 7, 2014, at 4:47 PM, Ethan Burns <burns...@gmail.com> wrote:

The first number is always subtracted. This will be included in the future documentation. If you are feeling ambition, I would gladly accept a patch documenting it. Otherwise, I'll get to it this weekend.

On Wed May 07 2014 at 7:32:25 PM, Brendan Tracey <tracey....@gmail.com> wrote:
Presumably it subtracts one of the values for you (given that they are both positive)? Is it always the first number that is subtracted (your code doesn’t show)? 

Thanks

On May 7, 2014, at 4:29 PM, Ethan Burns <burns...@gmail.com> wrote:

The error values are specified as two positive values that the plotter adds to the center for you:
package main

import (
)

type XYMeanErr []struct {
X float64
YMean, YError float64
}

func (e XYMeanErr) Len() int {
return len(e)
}

func (e XYMeanErr) XY(i int) (x, y float64) {
return e[i].X, e[i].YMean
}

func (e XYMeanErr) YError(i int) (float64, float64) {
yerr := e[i].YError
return yerr, yerr
}

func main() {
data := XYMeanErr{
{0, 10, 5}, {1, 10, 5}, {2, 20, 10},
}

p, err := plot.New()
if err != nil {
panic(err)
}
line, err := plotter.NewLine(data)
if err != nil {
panic(err)
}
yerr, err := plotter.NewYErrorBars(data)
if err != nil {
panic(err)
}
p.Add(line, yerr)
p.Y.Min = 0
if err := p.Save(4, 4, "test.eps"); err != nil {
panic(err)
}
}

This needs to be better documented, since it's no clear from any of the godoc. I had to look at how the plotter draws the lines to figure it out. Also, there should be an example. Here's the issue: https://code.google.com/p/plotinum/issues/detail?id=141.


Ethan

On Wed May 07 2014 at 7:05:10 PM, Brendan Tracey <tracey....@gmail.com> wrote:
And here is the plot plotinum is making. The values don’t match up. (at least with the Y axis location, though for this particular run the trend looks okay)
>> To unsubscribe from this group and stop receiving emails from it, send an email to plotinum-discuss+unsubscribe@googlegroups.com.

>> For more options, visit https://groups.google.com/d/optout.
>




---------- Forwarded message ----------
From: Brendan Tracey <tracey....@gmail.com>
To: Ethan Burns <burns...@gmail.com>
Cc: 
Date: Wed, 7 May 2014 16:05:07 -0700
Subject: Re: Plot with error bars
And here is the plot plotinum is making. The values don’t match up. (at least with the Y axis location, though for this particular run the trend looks okay)




---------- Forwarded message ----------
From: Brendan Tracey <tracey....@gmail.com>
To: Ethan Burns <burns...@gmail.com>
Cc: 
Date: Wed, 7 May 2014 16:05:07 -0700
Subject: Re: Plot with error bars

Ethan Burns

unread,
May 7, 2014, 8:25:51 PM5/7/14
to plotinum...@googlegroups.com
I'm annoyed. I don't like the ambiguity between whether the error values should be positive or negative. Certainly it's never correct to add two positive error values to the middle or to subtract two negative values. Both of these would make invalid error bars where both bars are on the same side of the middle. Currently. You can get this invalid behavior if you return a value with the wrong sign from the errorer interfaces. What if, instead, the plotter took the absolute value of both error values and subtracted the first from the middle and added the second. That way you can return either positive or negative values and you get the valid (if not correct) behavior. Also, it won't break any correct, currently-existing plotters.

Thoughts?


Ethan

Dan Kortschak

unread,
May 7, 2014, 8:48:17 PM5/7/14
to Ethan Burns, plotinum...@googlegroups.com
I think this is the best approach.

Ethan Burns

unread,
May 10, 2014, 10:41:22 AM5/10/14
to Dan Kortschak, plotinum...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages