Created
March 24, 2018 12:47
-
-
Save metal3d/599f19bc895db0ba828748ea4d2ddb87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Proposes to draw gonum plot on Jupyter using | |
| // the fantastic lgo kernel | |
| package jplot | |
| // You may save that file in $GOPATH/src/jplot/jplot.go | |
| import ( | |
| "bytes" | |
| "github.com/yunabe/lgo/core" | |
| "gonum.org/v1/plot" | |
| "gonum.org/v1/plot/vg" | |
| ) | |
| // Plot given gonum/plot with dimension | |
| func Plot(p *plot.Plot, dim ...vg.Length) { | |
| plt(p, dim...) | |
| } | |
| func plt(p *plot.Plot, dim ...vg.Length) { | |
| _ctx := core.GetExecContext() | |
| w := vg.Length(340) | |
| h := vg.Length(240) | |
| if len(dim) > 0 { | |
| w = dim[0] | |
| } | |
| if len(dim) > 1 { | |
| h = dim[1] | |
| } | |
| writer, err := p.WriterTo(w, h, "png") | |
| if err != nil { | |
| panic(err) | |
| } | |
| b := []byte{} | |
| buff := bytes.NewBuffer(b) | |
| _, err = writer.WriteTo(buff) | |
| if err != nil { | |
| panic(err) | |
| } | |
| _ctx.Display.PNG(buff.Bytes(), nil) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment