// Plots the historgram using plotinum
func Histogram(r RetCalc) {
	//eb := all_paths.End_balances()
	eb := make([]float64, len(r.All_paths), len(r.All_paths))
	incs := r.RunIncomes()
	for i := range incs {
		eb[i] = incs[i]
	}
	v := make(plotter.Values, len(eb))
	for i := range v {
		v[i] = eb[i]
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = "Histogram"
	h, err := plotter.NewHist(v, 100)
	if err != nil {
		panic(err)
	}
	//h.Normalize(1)
	p.Add(h)

	if err := p.Save(4, 4, "hist.png"); err != nil {
		panic(err)
	}
	fmt.Println(h)
}
Exemple #2
0
// Example_quartPlots draws vertical quartile plots.
func Example_quartPlots() *plot.Plot {
	rand.Seed(int64(0))
	n := 100
	uniform := make(plotter.Values, n)
	normal := make(plotter.Values, n)
	expon := make(plotter.Values, n)
	for i := 0; i < n; i++ {
		uniform[i] = rand.Float64()
		normal[i] = rand.NormFloat64()
		expon[i] = rand.ExpFloat64()
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = "Quartile Plot"
	p.Y.Label.Text = "plotter.Values"

	p.Add(must(plotter.NewQuartPlot(0, uniform)).(*plotter.QuartPlot),
		must(plotter.NewQuartPlot(1, normal)).(*plotter.QuartPlot),
		must(plotter.NewQuartPlot(2, expon)).(*plotter.QuartPlot))

	// Set the X axis of the plot to nominal with
	// the given names for x=0, x=1 and x=2.
	p.NominalX("Uniform\nDistribution", "Normal\nDistribution",
		"Exponential\nDistribution")
	return p
}
Exemple #3
0
func plotData(name string, us, ys, ts, fs []float64) {
	p, err := plot.New()
	if err != nil {
		fmt.Printf("Cannot create new plot: %s\n", err)
		return
	}
	p.Title.Text = "Least-square fit of convex function"
	p.X.Min = -0.1
	p.X.Max = 2.3
	p.Y.Min = -1.1
	p.Y.Max = 7.2
	p.Add(plotter.NewGrid())

	pts := plotter.NewScatter(dataset(us, ys))
	pts.GlyphStyle.Color = color.RGBA{R: 255, A: 255}

	fit := plotter.NewLine(dataset(ts, fs))
	fit.LineStyle.Width = vg.Points(1)
	fit.LineStyle.Color = color.RGBA{B: 255, A: 255}

	p.Add(pts)
	p.Add(fit)
	if err := p.Save(4, 4, name); err != nil {
		fmt.Printf("Save to '%s' failed: %s\n", name, err)
	}
}
Exemple #4
0
// Plot saves an image of the latency histogram to filePath. The extension of filePath defines
// the format to be used - png, svg, etc.
func Plot(h *latency.Histogram, description, filePath string) error {
	count := len(h.Buckets)
	xys := make(plotter.XYs, count)

	for bucket, freq := range h.Buckets {
		xys[bucket].X = float64(bucket)
		xys[bucket].Y = float64(freq)
	}

	p, err := plot.New()
	if err != nil {
		return fmt.Errorf("error generating plot: %v", err)
	}
	p.Title.Text = description
	p.X.Label.Text = fmt.Sprintf("Latency (%v resolution)", h.Resolution)
	p.Y.Label.Text = "Frequency"

	hh, err := plotter.NewHistogram(xys, count)
	if err != nil {
		return fmt.Errorf("error generating histogram: %v", err)

	}
	p.Add(hh)

	// Save the plot to a file. Units in inches (one inch == 72 points).
	fmt.Fprintf(os.Stderr, "Saving latency histogram to %v\n", filePath)
	return p.Save(8, 6, filePath)
}
Exemple #5
0
func test1() {
	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, 4, "points.png"); err != nil {
		panic(err)
	}
}
Exemple #6
0
func test_point() {
	rand.Seed(int64(0))
	points_data := randomPoint(200)
	points_data2 := randomPoint(50)
	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = "Points"
	p.X.Label.Text = "X"
	p.Y.Label.Text = "Y"

	bs, _ := plotter.NewBubbles(points_data, vg.Points(5), vg.Points(5))
	bs2, _ := plotter.NewBubbles(points_data2, vg.Points(5), vg.Points(5))

	bs.Color = color.RGBA{R: 255, G: 0, B: 0, A: 255}
	bs2.Color = color.RGBA{R: 0, G: 255, B: 0, A: 255}
	p.Add(bs)
	p.Add(bs2)

	if err := p.Save(10, 10, "points.png"); err != nil {
		panic(err)
	}
}
Exemple #7
0
func plotData(name string, xs, ys []float64) {
	p, err := plot.New()
	if err != nil {
		fmt.Printf("Cannot create new plot: %s\n", err)
		return
	}
	p.Title.Text = "Chernoff lower bound"
	p.X.Label.Text = "Sigma"
	p.X.Min = 0.2
	p.X.Max = 0.5
	p.Y.Label.Text = "Probability of correct detection"
	p.Y.Min = 0.9
	p.Y.Max = 1.0
	p.Add(plotter.NewGrid())

	l := plotter.NewLine(dataset(xs, ys))
	l.LineStyle.Width = vg.Points(1)
	//l.LineStyle.Dashes = []vg.Length(vg.Points(5), vg.Points(5))
	l.LineStyle.Color = color.RGBA{B: 255, A: 255}

	p.Add(l)
	if err := p.Save(4, 4, name); err != nil {
		fmt.Printf("Save to '%s' failed: %s\n", name, err)
	}
}
Exemple #8
0
// Example_boxPlots draws vertical boxplots.
func Example_boxPlots() *plot.Plot {
	rand.Seed(int64(0))
	n := 100
	uniform := make(plotter.Values, n)
	normal := make(plotter.Values, n)
	expon := make(plotter.Values, n)
	for i := 0; i < n; i++ {
		uniform[i] = rand.Float64()
		normal[i] = rand.NormFloat64()
		expon[i] = rand.ExpFloat64()
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = "Box Plot"
	p.Y.Label.Text = "plotter.Values"

	// Make boxes for our data and add them to the plot.
	p.Add(plotter.NewBoxPlot(vg.Points(20), 0, uniform),
		plotter.NewBoxPlot(vg.Points(20), 1, normal),
		plotter.NewBoxPlot(vg.Points(20), 2, expon))

	// Set the X axis of the plot to nominal with
	// the given names for x=0, x=1 and x=2.
	p.NominalX("Uniform\nDistribution", "Normal\nDistribution",
		"Exponential\nDistribution")
	return p
}
Exemple #9
0
// draw is a generic plotter of labelled lines.
func draw(lines graph, title, xLabel, yLabel string) error {
	p, err := plot.New()
	if err != nil {
		return fmt.Errorf(err.Error())
	}

	p.Title.Text = title
	p.X.Label.Text = xLabel
	p.Y.Label.Text = yLabel

	i := 0
	for legend, data := range lines {
		i = i + 1
		l, err := plotter.NewLine(xys(data))
		if err != nil {
			return fmt.Errorf(err.Error())
		}

		p.Add(l)
		p.Legend.Add(legend, l)
		l.LineStyle.Color = getColor(i)
	}

	if err != nil {
		return fmt.Errorf(err.Error())
	}

	name := strings.Replace(strings.ToLower(title), " ", "-", -1)
	filename := fmt.Sprintf("strategy-%s.svg", name)
	if err := p.Save(8, 8, filename); err != nil {
		return fmt.Errorf(err.Error())
	}

	return nil
}
Exemple #10
0
func Example_groupedHorizontalQuartPlots() *plot.Plot {
	rand.Seed(int64(0))
	n := 100
	uniform := make(plotter.Values, n)
	normal := make(plotter.Values, n)
	expon := make(plotter.Values, n)
	for i := 0; i < n; i++ {
		uniform[i] = rand.Float64()
		normal[i] = rand.NormFloat64()
		expon[i] = rand.ExpFloat64()
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = "Box Plot"
	p.Y.Label.Text = "plotter.Values"

	w := vg.Points(10)
	for x := 0.0; x < 3.0; x++ {
		b0 := must(plotter.MakeHorizQuartPlot(x, uniform)).(plotter.HorizQuartPlot)
		b0.Offset = -w
		b1 := must(plotter.MakeHorizQuartPlot(x, normal)).(plotter.HorizQuartPlot)
		b2 := must(plotter.MakeHorizQuartPlot(x, expon)).(plotter.HorizQuartPlot)
		b2.Offset = w
		p.Add(b0, b1, b2)
	}
	p.Add(plotter.NewGlyphBoxes())

	p.NominalY("Group 0", "Group 1", "Group 2")
	return p
}
Exemple #11
0
// Draw the plotinum logo.
func Example_logo() *plot.Plot {
	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	plotter.DefaultLineStyle.Width = vg.Points(1)
	plotter.DefaultGlyphStyle.Radius = vg.Points(3)

	p.Y.Tick.Marker = plot.ConstantTicks([]plot.Tick{
		{0, "0"}, {0.25, ""}, {0.5, "0.5"}, {0.75, ""}, {1, "1"},
	})
	p.X.Tick.Marker = plot.ConstantTicks([]plot.Tick{
		{0, "0"}, {0.25, ""}, {0.5, "0.5"}, {0.75, ""}, {1, "1"},
	})

	pts := plotter.XYs{{0, 0}, {0, 1}, {0.5, 1}, {0.5, 0.6}, {0, 0.6}}
	line := must(plotter.NewLine(pts)).(*plotter.Line)
	scatter := must(plotter.NewScatter(pts)).(*plotter.Scatter)
	p.Add(line, scatter)

	pts = plotter.XYs{{1, 0}, {0.75, 0}, {0.75, 0.75}}
	line = must(plotter.NewLine(pts)).(*plotter.Line)
	scatter = must(plotter.NewScatter(pts)).(*plotter.Scatter)
	p.Add(line, scatter)

	pts = plotter.XYs{{0.5, 0.5}, {1, 0.5}}
	line = must(plotter.NewLine(pts)).(*plotter.Line)
	scatter = must(plotter.NewScatter(pts)).(*plotter.Scatter)
	p.Add(line, scatter)

	return p
}
Exemple #12
0
// Example_errBars draws points and error bars.
func Example_errBars() *plot.Plot {

	type errPoints struct {
		plotter.XYs
		plotter.YErrors
		plotter.XErrors
	}

	rand.Seed(int64(0))
	n := 15
	data := errPoints{
		XYs:     randomPoints(n),
		YErrors: plotter.YErrors(randomError(n)),
		XErrors: plotter.XErrors(randomError(n)),
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	scatter := must(plotter.NewScatter(data)).(*plotter.Scatter)
	scatter.Shape = plot.CrossGlyph{}
	xerrs, err := plotter.NewXErrorBars(data)
	if err != nil {
		panic(err)
	}
	yerrs, err := plotter.NewYErrorBars(data)
	if err != nil {
		panic(err)
	}
	p.Add(scatter, xerrs, yerrs)
	p.Add(plotter.NewGlyphBoxes())

	return p
}
Exemple #13
0
// An example of making a histogram.
func Example_histogram() *plot.Plot {
	rand.Seed(int64(0))
	n := 10000
	vals := make(plotter.Values, n)
	for i := 0; i < n; i++ {
		vals[i] = rand.NormFloat64()
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = "Histogram"
	h, err := plotter.NewHist(vals, 16)
	if err != nil {
		panic(err)
	}
	h.Normalize(1)
	p.Add(h)

	// The normal distribution function
	norm := plotter.NewFunction(stdNorm)
	norm.Color = color.RGBA{R: 255, A: 255}
	norm.Width = vg.Points(2)
	p.Add(norm)

	return p
}
Exemple #14
0
func main() {
	// Get some random data.
	n, m := 5, 10
	pts := make([]plotter.XYer, n)
	for i := range pts {
		xys := make(plotter.XYs, m)
		pts[i] = xys
		center := float64(i)
		for j := range xys {
			xys[j].X = center + (rand.Float64() - 0.5)
			xys[j].Y = center + (rand.Float64() - 0.5)
		}
	}

	plt, err := plot.New()
	if err != nil {
		panic(err)
	}

	mean95 := plotutil.NewErrorPoints(plotutil.MeanAndConf95, pts...)
	medMinMax := plotutil.NewErrorPoints(plotutil.MedianAndMinMax, pts...)
	plotutil.AddLinePoints(plt,
		"mean and 95% confidence", mean95,
		"median and minimum and maximum", medMinMax)
	plotutil.AddErrorBars(plt, mean95, medMinMax)
	plotutil.AddScatters(plt, pts[0], pts[1], pts[2], pts[3], pts[4])

	plt.Save(4, 4, "errpoints.png")
}
Exemple #15
0
// createLine creates a line graph from provided x,y data and title
func createLine(xdat, ydat [][]float64, ylab []string, title string) {
	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Add(plotter.NewGrid())
	p.Title.Text = title
	p.Legend.Top = true
	p.Legend.XOffs = -10.0
	p.Legend.YOffs = -10.0

	var scatdata xyer
	var s *plotter.Line
	for i, _ := range ydat {
		scatdata = xyer{xdat[i], ydat[i]}
		s = plotter.NewLine(scatdata)
		s.LineStyle.Width = 2
		s.LineStyle.Color = cols[i]
		p.Add(s)
		p.Legend.Add(ylab[i], s)
	}
	p.X.Max = 2.5
	p.Y.Max = 3.5
	p.X.Label.Text = "Time / ps"
	p.Y.Label.Text = "Probability density"

	if err := p.Save(5, 5, "out/"+title+".png"); err != nil {
		panic(err)
	}
}
Exemple #16
0
func plotLine(xy plotter.XYs) (image.Image, error) {

	p, err := plot.New()
	if err != nil {
		return nil, err
	}
	p.HideAxes()
	p.BackgroundColor = &color.RGBA{0, 0, 0, 255}

	//s, err := NewSparkLines(xy)
	s, err := plotter.NewLine(xy)
	if err != nil {
		return nil, err
	}
	s.Color = &color.RGBA{0, 255, 0, 128}
	p.Add(s)

	// Draw the plot to an in-memory image.
	// _, rows, _ := terminal.GetSize(0)
	charWidth := optCharWidth
	charHeight := optCharHeight
	//width := cols * charWidth
	height := optRows * charHeight

	img := image.NewRGBA(image.Rect(0, 0, 5+(len(xy)*charWidth), height))
	canvas := vgimg.NewImage(img)
	da := plot.MakeDrawArea(canvas)
	p.Draw(da)

	return img, nil
}
Exemple #17
0
// Example_groupedBoxPlots draws vertical boxplots.
func Example_groupedBoxPlots() *plot.Plot {
	rand.Seed(int64(0))
	n := 100
	uniform := make(plotter.Values, n)
	normal := make(plotter.Values, n)
	expon := make(plotter.Values, n)
	for i := 0; i < n; i++ {
		uniform[i] = rand.Float64()
		normal[i] = rand.NormFloat64()
		expon[i] = rand.ExpFloat64()
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = "Box Plot"
	p.Y.Label.Text = "plotter.Values"

	w := vg.Points(20)
	for x := 0.0; x < 3.0; x++ {
		b0 := must(plotter.NewBoxPlot(w, x, uniform)).(*plotter.BoxPlot)
		b0.Offset = -w - vg.Points(3)
		b1 := must(plotter.NewBoxPlot(w, x, normal)).(*plotter.BoxPlot)
		b2 := must(plotter.NewBoxPlot(w, x, expon)).(*plotter.BoxPlot)
		b2.Offset = w + vg.Points(3)
		p.Add(b0, b1, b2)
	}

	// Set the X axis of the plot to nominal with
	// the given names for x=0, x=1 and x=2.
	p.NominalX("Group 0", "Group 1", "Group 2")
	return p
}
Exemple #18
0
// Example_functions draws some functions.
func Example_functions() *plot.Plot {
	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = "Functions"
	p.X.Label.Text = "X"
	p.Y.Label.Text = "Y"

	quad := plotter.NewFunction(func(x float64) float64 { return x * x })
	quad.Color = color.RGBA{B: 255, A: 255}

	exp := plotter.NewFunction(func(x float64) float64 { return math.Pow(2, x) })
	exp.Dashes = []vg.Length{vg.Points(2), vg.Points(2)}
	exp.Width = vg.Points(2)
	exp.Color = color.RGBA{G: 255, A: 255}

	sin := plotter.NewFunction(func(x float64) float64 { return 10*math.Sin(x) + 50 })
	sin.Dashes = []vg.Length{vg.Points(4), vg.Points(5)}
	sin.Width = vg.Points(4)
	sin.Color = color.RGBA{R: 255, A: 255}

	p.Add(quad, exp, sin)
	p.Legend.Add("x^2", quad)
	p.Legend.Add("2^x", exp)
	p.Legend.Add("10*sin(x)+50", sin)
	p.Legend.ThumbnailWidth = vg.Inches(0.5)

	p.X.Min = 0
	p.X.Max = 10
	p.Y.Min = 0
	p.Y.Max = 100
	return p
}
Exemple #19
0
func main() {
	var maxworker = *flag.Int("maxworkers", MAXWORKER, "Maxworker")
	var queryNum = *flag.Int("qpr", QN, "query per worker")
	flag.Parse()
	fmt.Println(maxworker, queryNum)
	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = "Query per second"
	p.X.Label.Text = "query number"
	p.Y.Label.Text = "query per second"
	pts := make(plotter.XYs, queryNum)
	for i := 10; i <= maxworker; i = i + 3 {
		for j := 1; j <= queryNum; j++ {
			val := mainLoop(i, j)
			pts[j-1].X = float64(j)
			pts[j-1].Y = val
		}
		err = plotutil.AddLinePointsColor(p, i, "Number of worker "+strconv.Itoa(i), pts)
		if err != nil {
			panic(err)
		}
	}
	if err := p.Save(4, 4, "points.png"); err != nil {
		panic(err)
	}
}
Exemple #20
0
func histPlot() *plot.Plot {
	// Draw some random values from the standard
	// normal distribution.
	rand.Seed(int64(0))
	v := make(plotter.Values, 1000)
	for i := range v {
		v[i] = rand.NormFloat64()
	}

	// Make a plot and set its title.
	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = "Histogram"

	// Create a histogram of our values drawn
	// from the standard normal.
	h, err := plotter.NewHist(v, 16)
	if err != nil {
		panic(err)
	}
	// Normalize the area under the histogram to
	// sum to one.
	h.Normalize(1)
	p.Add(h)

	// The normal distribution function
	norm := plotter.NewFunction(stdNorm)
	norm.Color = color.RGBA{R: 255, A: 255}
	norm.Width = vg.Points(2)
	p.Add(norm)
	return p
}
Exemple #21
0
func main() {
	fmt.Println("Running...")
	start := time.Now()

	A2 = make([]float64, 2)
	A3 = make([]float64, 3)

	A1 = 1
	A2[0] = 1.0 / (3.0 * LaguerreD(2, z2[0]) * Laguerre(3, z2[0]))
	A2[1] = 1.0 / (3.0 * LaguerreD(2, z2[1]) * Laguerre(3, z2[1]))

	A3[0] = 1.0 / (4.0 * LaguerreD(3, z3[0]) * Laguerre(4, z3[0]))
	A3[1] = 1.0 / (4.0 * LaguerreD(3, z3[1]) * Laguerre(4, z3[1]))
	A3[2] = 1.0 / (4.0 * LaguerreD(3, z3[2]) * Laguerre(4, z3[2]))

	pt = make(plotter.XYs, nPlot)
	x = make([]float64, nPlot)

	dx := (xmax - xmin) / float64(nPlot-1)
	for i := range x {
		x[i] = dx*float64(i) + xmin
		pt[i].X = x[i]
		pt[i].Y = (math.Gamma(x[i]))
	}

	var p1, p2, p3 plotter.XYs

	p1 = make(plotter.XYs, nPlot)
	p2 = make(plotter.XYs, nPlot)
	p3 = make(plotter.XYs, nPlot)

	for i := range x {
		p1[i].X = x[i]
		p2[i].X = x[i]
		p3[i].X = x[i]
		p1[i].Y = (A1 * math.Pow(z1, x[i]-1))
		p2[i].Y = (A2[0]*math.Pow(z2[0], x[i]-1) + A2[1]*math.Pow(z2[1], x[i]-1))
		p3[i].Y = (A3[0]*math.Pow(z3[0], x[i]-1) + A3[1]*math.Pow(z3[1], x[i]-1) + A3[2]*math.Pow(z3[2], x[i]-1))
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = fmt.Sprintf("Gamma Function Approximations")
	p.Y.Label.Text = "Log(y)"
	p.X.Label.Text = "x"

	plotutil.AddLinePoints(p, "Log(Gamma)", pt, "m=0", p1, "m=1", p2, "m=2", p3)

	// Save the plot to a PNG file.
	if err := p.Save(6, 4, "gammaLow.png"); err != nil {
		panic(err)
	}

	fmt.Println(time.Since(start))
	fmt.Println("...program terminated successfully!")
}
Exemple #22
0
func main() {
	if len(os.Args) != 2 {
		fmt.Fprintf(os.Stderr, "Usage: simpleRead <file.wav>\n")
		os.Exit(1)
	}

	// open file
	testInfo, err := os.Stat(os.Args[1])
	checkErr(err)

	testWav, err := os.Open(os.Args[1])
	checkErr(err)

	wavReader, err := wav.NewWavReader(testWav, testInfo.Size())
	checkErr(err)

	// File informations
	fmt.Println(wavReader)

	// limit sample count
	sampleCnt := wavReader.GetSampleCount()
	if sampleCnt > 10000 {
		sampleCnt = 10000
	}

	// setup plotter
	p, err := plot.New()
	checkErr(err)

	p.Title.Text = "Waveplot"
	p.X.Label.Text = "t"
	p.Y.Label.Text = "Ampl"

	pts := make(plotter.XYs, sampleCnt)

	// read samples and construct points for plot
	for i := range pts {
		n, err := wavReader.ReadSample()
		if err == io.EOF {
			break
		}
		checkErr(err)

		pts[i].X = float64(i)
		pts[i].Y = float64(n)
	}

	err = plotutil.AddLinePoints(p, "", pts)
	checkErr(err)

	// construct output filename
	inputFname := path.Base(os.Args[1])
	plotFname := strings.Split(inputFname, ".")[0] + ".png"

	if err := p.Save(10, 4, plotFname); err != nil {
		panic(err)
	}
}
Exemple #23
0
func main() {
	fmt.Println("Running...")
	start := time.Now()

	count := 0
	pt = make([]plotter.XYs, 0)
	for n := nMin; n <= nMax; n *= 2 {
		y1 = make([]float64, n)
		y2 = make([]float64, n)
		pt = append(pt, make(plotter.XYs, n))

		y1[0] = 1.0
		y2[0] = 3.0
		h = (tMax - tMin) / float64(n-1)

		for i := 1; i < n; i++ {
			y1[i] = y1[i-1] + 2*y1[i-1]*(1-y2[i-1])*h
			y2[i] = y2[i-1] - y2[i-1]*(1-y1[i-1])*h
		}

		for i := 0; i < n; i++ {
			pt[count][i].X = y1[i]
			pt[count][i].Y = y2[i]
		}
		count++
	}
	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = fmt.Sprintf("Enright and Pryce #B1:Euler")
	p.Y.Label.Text = "y2(t)"
	p.X.Label.Text = "y1(t)"

	n := nMin
	for i := 0; i < count; i++ {
		l := plotter.NewLine(pt[i])
		l.LineStyle.Width = vg.Points(1)
		l.LineStyle.Color = color.RGBA{R: 255 / uint8(i+1), G: 255 / uint8(i+1),
			B: 255 / uint8(i+1), A: 255}
		p.Add(l)
		n *= 2
	}
	p.X.Min = 0
	p.X.Max = 6.5
	p.Y.Min = 0
	p.Y.Max = 6.5

	// Save the plot to a PNG file.
	if err := p.Save(6, 6, "euler_test.png"); err != nil {
		panic(err)
	}

	fmt.Println(time.Since(start))
	fmt.Println("...program terminated successfully!")
}
Exemple #24
0
func DataTableToPng(b *bytes.Buffer, dt *db.DataTable, title string, width, height float64, xLabel string) error {
	p, err := plot.New()
	if err != nil {
		return err
	}

	p.Title.Text = title
	p.X.Label.Text = xLabel
	p.Y.Label.Text = "msec" // TODO: Fix this.

	// TODO: need new ticker function to handle equalX (while keeping xLabel as selected)
	if xLabel == common.TimeName {
		p.X.Tick.Marker = TimeTicks
	}
	p.Legend.Top = true

	numColumns := len(dt.ColumnNames)
	lines := make([]plotter.XYs, numColumns-1) // Skip X column.

	for _, dRow := range dt.Data {
		xp := (*dRow)[0]
		if xp != nil {
			for col := 1; col < numColumns; col++ { // Skip X column.
				yp := (*dRow)[col]
				if yp != nil {
					lines[col-1] = append(lines[col-1], struct{ X, Y float64 }{X: *xp, Y: *yp})
				}
			}
		}
	}

	colorList := getColors(numColumns - 1) // Skip X column.

	for i, line := range lines {
		columnName := dt.ColumnNames[i+1]
		l, err := plotter.NewLine(line)
		if err != nil {
			return err
		}
		if strings.Index(columnName, common.RegressNamePrefix) == 0 { // If regression value.
			l.LineStyle.Color = color.RGBA{255, 0, 0, 255}
			l.LineStyle.Width = vg.Points(2.0)
		} else {
			l.LineStyle.Color = colorList[i]
			l.LineStyle.Width = vg.Points(1.5)
		}
		p.Add(l)
		p.Legend.Add(columnName, l)
	}

	tPng := time.Now()
	drawPng(b, p, width, height)
	glog.V(3).Infof("PERF: makePng time: %v", time.Now().Sub(tPng))
	return nil
}
Exemple #25
0
func linesPlot() *plot.Plot {
	// Get some random points
	rand.Seed(int64(0))
	n := 10
	scatterData := randomPoints(n)
	lineData := randomPoints(n)
	linePointsData := 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}

	// Make a line plotter and set its style.
	l, err := plotter.NewLine(lineData)
	if err != nil {
		panic(err)
	}
	l.LineStyle.Width = vg.Points(1)
	l.LineStyle.Dashes = []vg.Length{vg.Points(5), vg.Points(5)}
	l.LineStyle.Color = color.RGBA{B: 255, A: 255}

	// Make a line plotter with points and set its style.
	lpLine, lpPoints, err := plotter.NewLinePoints(linePointsData)
	if err != nil {
		panic(err)
	}
	lpLine.Color = color.RGBA{G: 255, A: 255}
	lpPoints.Shape = plot.PyramidGlyph{}
	lpPoints.Color = color.RGBA{R: 255, A: 255}

	// Add the plotters to the plot, with a legend
	// entry for each
	p.Add(s, l, lpLine, lpPoints)
	p.Legend.Add("scatter", s)
	p.Legend.Add("line", l)
	p.Legend.Add("line points", lpLine, lpPoints)
	return p
}
Exemple #26
0
// Example_horizontalQuartPlots draws horizontal quartile plots
// with some labels on their points.
func Example_horizontalQuartPlots() *plot.Plot {
	rand.Seed(int64(0))
	n := 100
	uniform := make(valueLabels, n)
	normal := make(valueLabels, n)
	expon := make(valueLabels, n)
	for i := 0; i < n; i++ {
		uniform[i].Value = rand.Float64()
		uniform[i].Label = fmt.Sprintf("%4.4f", uniform[i].Value)
		normal[i].Value = rand.NormFloat64()
		normal[i].Label = fmt.Sprintf("%4.4f", normal[i].Value)
		expon[i].Value = rand.ExpFloat64()
		expon[i].Label = fmt.Sprintf("%4.4f", expon[i].Value)
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = "Horizontal Quartile Plot"
	p.X.Label.Text = "plotter.Values"

	// Make boxes for our data and add them to the plot.
	uniBox := must(plotter.MakeHorizQuartPlot(0, uniform)).(plotter.HorizQuartPlot)
	uniLabels, err := uniBox.OutsideLabels(uniform)
	if err != nil {
		panic(err)
	}

	normBox := must(plotter.MakeHorizQuartPlot(1, normal)).(plotter.HorizQuartPlot)
	normLabels, err := normBox.OutsideLabels(normal)
	if err != nil {
		panic(err)
	}

	expBox := must(plotter.MakeHorizQuartPlot(2, expon)).(plotter.HorizQuartPlot)
	expLabels, err := expBox.OutsideLabels(expon)
	if err != nil {
		panic(err)
	}
	p.Add(uniBox, uniLabels, normBox, normLabels, expBox, expLabels)

	// Add a GlyphBox plotter for debugging.
	p.Add(plotter.NewGlyphBoxes())

	// Set the Y axis of the plot to nominal with
	// the given names for y=0, y=1 and y=2.
	p.NominalY("Uniform\nDistribution", "Normal\nDistribution",
		"Exponential\nDistribution")
	return p
}
Exemple #27
0
func main() {
	tests := []*Test{}
	// if file, err := os.Open("results.txt"); err != nil {
	if file, err := os.Stdin, error(nil); err != nil {
		log.Fatal(err)
	} else {
		scanner := bufio.NewScanner(file)
		for scanner.Scan() {
			test, err := ParseLine(scanner.Text())
			if err != nil {
				continue
			}
			test.Calculate()
			tests = append(tests, test)
		}
	}
	if len(tests) == 0 {
		log.Fatal("No tests found.")
	}
	if plt, err := plot.New(); err != nil {
		log.Fatal(err)
	} else {
		for i, test := range tests {
			xys := test.ToXY(100000.0)
			line, err := plotter.NewLine(xys)
			if err != nil {
				log.Fatal(err)
			}
			line.Color, line.Dashes = plotutil.Color(i), plotutil.Dashes(0)
			plt.Add(line)
			{
				name := fmt.Sprintf("%s (%d)", test.Name, int(test.Stats.Mean/100000))
				plt.Legend.Add(name, line)
			}
		}
		// plt.Legend.Font.Size = vg.Inches(plt.Legend.Font.Size * 0.75)
		plt.Legend.Font.Size *= 0.65
		plt.Legend.Top = true
		plt.Legend.Left = true
		// plt.Legend.YOffs = vg.Inches(-1.12)
		// plt.Legend.XOffs = vg.Inches(1)
		plt.HideX()
		// plt.X.Padding = 20
		plt.Y.Min = 0
		plt.Y.Max = 1600
		plt.Title.Text = fmt.Sprintf("Speed Test (averaged over %d runs and %d tests)", 100000, 100)
		plt.Y.Label.Text = "Cycles"
		plt.Save(6, 4, "number-parse.svg")
	}
}
Exemple #28
0
func GenerateGraph(teamName string, pythagorean, reals []float64) {
	p, _ := plot.New()
	p.Title.Text = teamName
	p.X.Label.Text = "Day"
	p.Y.Label.Text = "Parcentage"
	p.X.Min = 0.0
	p.X.Max = 38.0
	p.Y.Min = 0.0
	p.Y.Max = 100.0
	plotutil.AddLinePoints(p, "", GeneratePlot(pythagorean), GeneratePlot(reals))
	width := 4.0
	height := 4.0
	p.Save(width, height, fmt.Sprintf("%s.png", teamName))
}
Exemple #29
0
func main() {
	fmt.Println("Running...")
	start := time.Now()

	ev = make([]float64, 0)

	for n := nmin; n <= nmax; n *= 2 {
		sum := 0.0
		dx := (xmax - xmin) / float64(n)
		dy := (ymax - ymin) / float64(n)
		y = make([]float64, n)
		x = make([]float64, n)
		for k := 0; k < n; k++ {
			x[k] = float64(k)*dx + 0.5*dx + xmin
			y[k] = (float64(k)*dy + 0.5*dy + ymin)
		}
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				sum += math.Exp(x[i]*y[j]) * dx * dy
			}
		}
		ev = append(ev, EXACT_VALUE-sum)
	}

	var pt plotter.XYs
	pt = make(plotter.XYs, len(ev))
	for i := range pt {
		pt[i].X = math.Log10(float64(nmin) * math.Pow(2, float64(i)))
		pt[i].Y = math.Log10(math.Abs(ev[i]))
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = fmt.Sprintf("Error of Midpoint Integral")
	p.X.Label.Text = "n"
	p.Y.Label.Text = "Log(|err|)"

	plotutil.AddLinePoints(p, pt)

	// Save the plot to a PNG file.
	if err := p.Save(6, 4, "error_hw12.png"); err != nil {
		panic(err)
	}

	fmt.Println(time.Since(start))
	fmt.Println("...program terminated successfully!")
}
Exemple #30
0
// Example_verticalBoxPlots draws vertical boxplots
// with some labels on their points.
func Example_verticalBoxPlots() *plot.Plot {
	rand.Seed(int64(0))
	n := 100
	uniform := make(valueLabels, n)
	normal := make(valueLabels, n)
	expon := make(valueLabels, n)
	for i := 0; i < n; i++ {
		uniform[i].Value = rand.Float64()
		uniform[i].Label = fmt.Sprintf("%4.4f", uniform[i].Value)
		normal[i].Value = rand.NormFloat64()
		normal[i].Label = fmt.Sprintf("%4.4f", normal[i].Value)
		expon[i].Value = rand.ExpFloat64()
		expon[i].Label = fmt.Sprintf("%4.4f", expon[i].Value)
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = "Box Plot"
	p.Y.Label.Text = "plotter.Values"

	// Make boxes for our data and add them to the plot.
	uniBox := must(plotter.NewBoxPlot(vg.Points(20), 0, uniform)).(*plotter.BoxPlot)
	uniLabels, err := uniBox.OutsideLabels(uniform)
	if err != nil {
		panic(err)
	}

	normBox := must(plotter.NewBoxPlot(vg.Points(20), 1, normal)).(*plotter.BoxPlot)
	normLabels, err := normBox.OutsideLabels(normal)
	if err != nil {
		panic(err)
	}

	expBox := must(plotter.NewBoxPlot(vg.Points(20), 2, expon)).(*plotter.BoxPlot)
	expLabels, err := expBox.OutsideLabels(expon)
	if err != nil {
		panic(err)
	}

	p.Add(uniBox, uniLabels, normBox, normLabels, expBox, expLabels)

	// Set the X axis of the plot to nominal with
	// the given names for x=0, x=1 and x=2.
	p.NominalX("Uniform\nDistribution", "Normal\nDistribution",
		"Exponential\nDistribution")
	return p
}