Created
April 7, 2016 11:22
-
-
Save sbinet/602522b7399ead414e279e2261d81095 to your computer and use it in GitHub Desktop.
gonum scatter plot
This file contains 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
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 | |
} |
Author
sbinet
commented
Apr 7, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment