Skip to content

Instantly share code, notes, and snippets.

@linw1995
Created June 4, 2017 04:53
Show Gist options
  • Save linw1995/d3b5440e9fbab81f311c0434a68f331b to your computer and use it in GitHub Desktop.
Save linw1995/d3b5440e9fbab81f311c0434a68f331b to your computer and use it in GitHub Desktop.
Example plots of "gonum/plot"
package main
import (
"math/rand"
"github.com/gonum/plot"
"github.com/gonum/plot/plotter"
"github.com/gonum/plot/plotutil"
"github.com/gonum/plot/vg"
)
func main() {
rand.Seed(int64(0))
p, err := plot.New()
if err != nil {
panic(err)
}
p.Title.Text = "Plotutil example"
p.X.Label.Text = "X"
p.Y.Label.Text = "Y"
err = plotutil.AddLinePoints(p,
"First", randomPoints(15),
"Second", randomPoints(15),
"Third", randomPoints(15))
if err != nil {
panic(err)
}
// Save the plot to a PNG file.
if err := p.Save(4*vg.Inch, 4*vg.Inch, "points.png"); err != nil {
panic(err)
}
}
// randomPoints returns some random x, y points.
func randomPoints(n int) plotter.XYs {
pts := make(plotter.XYs, n)
for i := range pts {
if i == 0 {
pts[i].X = rand.Float64()
} else {
pts[i].X = pts[i-1].X + rand.Float64()
}
pts[i].Y = pts[i].X + 10*rand.Float64()
}
return pts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment