Skip to content

Instantly share code, notes, and snippets.

@sbinet
Created April 7, 2016 11:22
Show Gist options
  • Save sbinet/602522b7399ead414e279e2261d81095 to your computer and use it in GitHub Desktop.
Save sbinet/602522b7399ead414e279e2261d81095 to your computer and use it in GitHub Desktop.
gonum scatter plot
package main
import (
"image/color"
"math/rand"
"github.com/gonum/plot"
"github.com/gonum/plot/plotter"
"github.com/gonum/plot/vg"
)
func main() {
// Get some random points
rand.Seed(int64(0))
n := 15
scatterData := randomPoints(n)
// Create a new plot, set its title and
// axis labels.
p, err := plot.New()
if err != nil {
panic(err)
}
p.Title.Text = "Points Example"
p.X.Label.Text = "X"
p.Y.Label.Text = "Y"
// Draw a grid behind the data
p.Add(plotter.NewGrid())
// Make a scatter plotter and set its style.
s, err := plotter.NewScatter(scatterData)
if err != nil {
panic(err)
}
s.GlyphStyle.Color = color.RGBA{R: 255, B: 128, A: 255}
p.Add(s)
// Save the plot to a PNG file.
if err := p.Save(4*vg.Inch, 4*vg.Inch, "scatter.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
}
@sbinet
Copy link
Author

sbinet commented Apr 7, 2016

scatter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment