plotter.NewLine() and NaN

54 views
Skip to first unread message

giancarlo

unread,
Sep 28, 2022, 7:30:25 PM9/28/22
to gonum-dev
Looking to plot multiple lines a graph. Something like the image below 1_rjhULfRfniAcRTAK9uSLNA.png
one consistent line (blue) and two other lines that cut off if they have no Y value corresponding to the X value.

currently i have something like this 

```
func makeXYs(x, y []float64) (plotter.XYs, error) {
  var XYs plotter.XYs
  if len(x) == len(y) {
    log.Debug("x and y match length. Creating X and Y points for graph")

    for i, _ := range x {
      XYs = append(XYs, plotter.XY{X: x[i], Y: y[i]})
    }

    return XYs, nil
  } else {
    return nil, fmt.Errorf("x:( %v )and y:( %v ) DON'T match length", x, y)
  }

}
```
```
dateX := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
blueY := []float64{15, 13, 19,  14, 11 ,13, 12, 17, 18, 20 }
redY := []float64{21, 23, 29, NaN NaN, NaN, NaN, 24, 25 ,26}
greenY := []float64{NaN, NaN, NaN, 5, 3, 9, 4, NaN, NaN, NaN}

blueXYs, _ := makeXYs(dateX, blueY)
redXYs, _ := makeXYs(dateX, redY)
greenXYs, _ := makeXYs(dateX, greenY)

p := plot.New()
blueLine, _ := plotter.NewLine(blueXYs)
redLine, err := plotter.NewLine(redXYs)
if err != nil {
    log.Error(err)
}

greenLine, err := plotter.NewLine(greenXYs)
if err != nil {
    log.Error(err)
}

p.Add(blueXYs, redXYs, greenXYs)
```
OUTPUT:

```
ERRO[0000] plotter: NaN data point
ERRO[0000] plotter: NaN data point

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x103017bf8]

goroutine 1 [running]:
gonum.org/v1/plot/plotter.(*Line).Len(0x0)
    <autogenerated>:1 +0x28
gonum.org/v1/plot/plotter.Range({0x103320d30, 0x140004d34b0})
    /Users/gespi/go/pkg/mod/gonum.org/v1/pl...@v0.11.0/plotter/plotter.go:58 +0xc8
gonum.org/v1/plot/plotter.XYRange({0x103320560, 0x0})
    /Users/gespi/go/pkg/mod/gonum.org/v1/pl...@v0.11.0/plotter/plotter.go:125 +0x5c
gonum.org/v1/plot/plotter.(*Line).DataRange(0x0)
    /Users/gespi/go/pkg/mod/gonum.org/v1/pl...@v0.11.0/plotter/line.go:144 +0x34
gonum.org/v1/plot.(*Plot).Add(0x14000265400, {0x140000a7c68, 0x3, 0x3})
    /Users/gespi/go/pkg/mod/gonum.org/v1/pl...@v0.11.0/plot.go:127 +0x17c
goblin-trader/pkg/plotty.SuperTrend(0x1400000e018, {0x1400022f800, 0x71, 0x80}, {0x1400022fc00, 0x71, 0x80})
    /Users/gespi/github/goblin-trader/pkg/plotty/supertrend.go:68 +0x6b0
goblin-trader/cmd.glob..func1(0x103b93960, {0x1400012ef60, 0x0, 0x6})
    /Users/gespi/github/goblin-trader/cmd/run.go:32 +0x158
github.com/spf13/cobra.(*Command).execute(0x103b93960, {0x1400012ef00, 0x6, 0x6})
    /Users/gespi/go/pkg/mod/github.com/spf13/co...@v1.5.0/command.go:876 +0x65c
github.com/spf13/cobra.(*Command).ExecuteC(0x103b936e0)
    /Users/gespi/go/pkg/mod/github.com/spf13/co...@v1.5.0/command.go:990 +0x410
github.com/spf13/cobra.(*Command).Execute(...)
    /Users/gespi/go/pkg/mod/github.com/spf13/co...@v1.5.0/command.go:918
goblin-trader/cmd.Execute()
    /Users/gespi/github/goblin-trader/cmd/root.go:33 +0x6c
main.main()
    /Users/gespi/github/goblin-trader/main.go:10 +0x20
exit status 2


I do not know how NaNs are supposed to be handled when plotting using plotter. I am not a data science specialist nor even a novice at it. So any help on how to produce lines that are cut off would be appreciated. I tried switching the NaNs to Zeros but they dont cut off the line and it still results in one line with the lines touching zero points. 

 

Dan Kortschak

unread,
Sep 30, 2022, 4:44:47 PM9/30/22
to gonu...@googlegroups.com
On Wed, 2022-09-28 at 16:30 -0700, giancarlo wrote:
>
> I do not know how NaNs are supposed to be handled when plotting using
> plotter. I am not a data science specialist nor even a novice at it.
> So any help on how to produce lines that are cut off would be
> appreciated. I tried switching the NaNs to Zeros but they dont cut
> off the line and it still results in one line with the lines touching
> zero points. 

NaN and Inf are not allowed in sequences of floats for plotting in the
general case, and certainly not in the Line plotter.

What I would do for your data if you will be having NaN/Inf in the
inputs would be to wrap plotter.NewLine so that you take a plotter.XYs
and return a []plotter.XYs that splits the sequences when NaN/Inf
appear. These would then be handed to plotter.NewLine to get a
[]plotter.Line that you would Add to the plot.Plot as a group.

This might seem involved, but with a helper function should not be too
much of an issue.

Sebastian may have a better solution.

Dan

giancarlo

unread,
Oct 3, 2022, 6:45:47 PM10/3/22
to gonum-dev
this is a route I have considered but not sure if this was the official way to do this. Good to hear from the community before i make some moves. TY 

Will await sebastions suggestions as well :) 

Sebastien Binet

unread,
Oct 4, 2022, 4:19:58 AM10/4/22
to giancarlo, gonum-dev
On Tue Oct 4, 2022 at 00:45 CET, giancarlo wrote:
> this is a route I have considered but not sure if this was the official
> way
> to do this. Good to hear from the community before i make some moves. TY
>
> Will await sebastions suggestions as well :)

I don't have much to add.
perhaps we could export a helper to break a plotter.Line/plotter.XYs into
a set of plotters (ie: a []plotter.Line/[]plotter.XYs)

this could perhaps be generalized with the help of generics (to turn a
[]T where T implement plot.Plotter, into a plot.Plotter)


hth,
-s

>
> On Friday, September 30, 2022 at 4:44:47 PM UTC-4 Dan Kortschak wrote:
>
> > On Wed, 2022-09-28 at 16:30 -0700, giancarlo wrote:
> > >
> > > I do not know how NaNs are supposed to be handled when plotting using
> > > plotter. I am not a data science specialist nor even a novice at it.
> > > So any help on how to produce lines that are cut off would be
> > > appreciated. I tried switching the NaNs to Zeros but they dont cut
> > > off the line and it still results in one line with the lines touching
> > > zero points.
> >
> > NaN and Inf are not allowed in sequences of floats for plotting in the
> > general case, and certainly not in the Line plotter.
> >
> > What I would do for your data if you will be having NaN/Inf in the
> > inputs would be to wrap plotter.NewLine so that you take a plotter.XYs
> > and return a []plotter.XYs that splits the sequences when NaN/Inf
> > appear. These would then be handed to plotter.NewLine to get a
> > []plotter.Line that you would Add to the plot.Plot as a group.
> >
> > This might seem involved, but with a helper function should not be too
> > much of an issue.
> >
> > Sebastian may have a better solution.
> >
> > Dan
> >
> >
>
> --
> You received this message because you are subscribed to the Google
> Groups "gonum-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to gonum-dev+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/gonum-dev/6cc5176d-0876-4fcb-bbdb-fea7c9f18321n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages