// 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) } }
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) } }
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) } }
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 }
// createHistogram creates a histogram from given data and bin numbers func createHistogram(data []float64, n int) { p, err := plot.New() if err != nil { panic(err) } p.Add(plotter.NewGrid()) histdata := valuer{data} p.Add(plotter.NewHist(histdata, n)) p.X.Label.Text = "time / ps" p.Y.Label.Text = "frequency" p.Title.Text = fmt.Sprintf("Frequency of lifetime data from lifetime.txt. %v bins.", n) if err := p.Save(5, 5, fmt.Sprintf("out/Histogram with %v bins.png", n)); err != nil { panic(err) } }
func PlotBands(symbol string, all []bands.Band) { dclose := make(plotter.XYs, len(all)) dsma := make(plotter.XYs, len(all)) dup := make(plotter.XYs, len(all)) ddown := make(plotter.XYs, len(all)) for i, b := range all { dclose[i].X = float64(-1 * i) dclose[i].Y = b.Close dsma[i].X = float64(-1 * i) dsma[i].Y = b.SMA dup[i].X = float64(-1 * i) dup[i].Y = b.Up ddown[i].X = float64(-1 * i) ddown[i].Y = b.Down } p, _ := plot.New() p.Title.Text = fmt.Sprintf("Bollinger Bands: %s", symbol) p.X.Label.Text = "Time (Days)" p.Y.Label.Text = "Value" p.Add(plotter.NewGrid()) lclose, _ := plotter.NewLine(dclose) lsma, _ := plotter.NewLine(dsma) lsma.LineStyle.Color = color.RGBA{B: 255, A: 255} lup, _ := plotter.NewLine(dup) lup.LineStyle.Color = color.RGBA{R: 255, A: 255} ldown, _ := plotter.NewLine(ddown) ldown.LineStyle.Color = color.RGBA{G: 255, A: 255} p.Add(lclose, lsma, lup, ldown) p.Legend.Add("Closing", lclose) p.Legend.Add("SMA", lsma) p.Legend.Add("Up", lup) p.Legend.Add("Down", ldown) p.Save(16, 9, fmt.Sprintf("%s.png", symbol)) }
func basicRamaPlot(title string) (*plot.Plot, error) { p, err := plot.New() if err != nil { return nil, err } p.Title.Padding = vg.Millimeters(3) p.Title.Text = title //"Ramachandran plot" p.X.Label.Text = "Phi" p.Y.Label.Text = "Psi" //Constant axes p.X.Min = -180 p.X.Max = 180 p.Y.Min = -180 p.Y.Max = 180 p.Add(plotter.NewGrid()) return p, nil }
// createScatter creates a scatter graph from provided x,y data and title func createScatter(xdat, ydat []float64, title string) { p, err := plot.New() if err != nil { panic(err) } p.Add(plotter.NewGrid()) p.Title.Text = title scatdata := xyer{xdat, ydat} s := plotter.NewScatter(scatdata) s.GlyphStyle.Radius = 2 s.GlyphStyle.Shape = &plot.BoxGlyph{} s.GlyphStyle.Color = color.RGBA{G: 100, A: 255} p.Add(s) if err := p.Save(5, 5, "out/"+title+".png"); err != nil { panic(err) } }
// Example_points draws some scatter points, a line, // and a line with points. func Example_points() *plot.Plot { rand.Seed(int64(0)) n := 15 scatterData := randomPoints(n) lineData := randomPoints(n) linePointsData := randomPoints(n) p, err := plot.New() if err != nil { panic(err) } p.Title.Text = "Points Example" p.X.Label.Text = "X" p.Y.Label.Text = "Y" p.Add(plotter.NewGrid()) s := must(plotter.NewScatter(scatterData)).(*plotter.Scatter) s.GlyphStyle.Color = color.RGBA{R: 255, B: 128, A: 255} s.GlyphStyle.Radius = vg.Points(3) l := must(plotter.NewLine(lineData)).(*plotter.Line) 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} lpLine, lpPoints, err := plotter.NewLinePoints(linePointsData) if err != nil { panic(err) } lpLine.Color = color.RGBA{G: 255, A: 255} lpPoints.Shape = plot.CircleGlyph{} lpPoints.Color = color.RGBA{R: 255, A: 255} 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 }
// Function called by rest of code to plot graphs func Plot(fname string, title string, labels []string, xdata []float64, ydata ...[]float64) { if len(fname) == 0 { fname = "output" } fname = fmt.Sprintf("out/%v h=%v m=%v M=%v l=%v γ=%v.png", fname, h, m, M, l, gamma) p, err := plot.New() if err != nil { panic(err) } fmt.Print(">> Plotting \"" + title + "\" to \"" + fname + "\"") // Add grid p.Add(plotter.NewGrid()) p.Title.Text = title p.Title.Padding = 10.0 p.X.Label.Text = labels[0] p.Y.Label.Text = labels[1] p.Legend.Top = true p.Legend.Left = true p.Legend.YOffs = -5.0 p.Legend.YOffs = -15.0 p.Legend.Padding = 10.0 p.Legend.ThumbnailWidth = 30.0 var s *plotter.Scatter // This is to allow for plotting of vertical lines for stability graphs stabtest := false last := len(ydata) if len(labels) == last { stabtest = true last -= 2 } // Loop through data and for i := 0; i < last; i++ { s = plotter.NewScatter(&_points{xdata, ydata[i]}) s.GlyphStyle.Color = cols[i] s.GlyphStyle.Radius = 1 s.GlyphStyle.Shape = plot.CircleGlyph{} p.Add(s) if len(ydata) > 1 { p.Legend.Add(labels[i+2], s) } } if stabtest { s = plotter.NewScatter(&_points{ydata[1], ydata[2]}) s.GlyphStyle.Color = cols[0] s.GlyphStyle.Radius = 1 s.GlyphStyle.Shape = plot.CircleGlyph{} p.Add(s) } // Save the plot to a PNG file. if err := p.Save(7, 7, fname); err != nil { panic(err) } fmt.Print(" ... Done\n") }
func TestServiceEpsilonGreedy(t *testing.T) { var ( horizon = 250 epsilons = []float32{0.1, 0.5, 0.9} hosts = map[string]bernouilliExperiment{ echo1: 0.1, echo2: 0.1, echo3: 0.9, } ) e1 := newEchoServer(t, echo1) defer e1.close() e2 := newEchoServer(t, echo2) defer e2.close() e3 := newEchoServer(t, echo3) defer e3.close() p, err := plot.New() if err != nil { t.Fatal(err) } p.Add(plotter.NewGrid()) p.X.Label.Text = "trials" p.Y.Label.Text = "average score" p.X.Max = float64(horizon) p.Y.Min = 0 p.Y.Max = 1 for _, e := range epsilons { means := make(plotter.XYs, horizon) s := NewService("echo", &ServiceConfig{ BanditStrategy: NewEpsilonGreedy(e), MemoizeScoreDuration: 1 * time.Millisecond, }) s.Add(echo1) s.Add(echo2) s.Add(echo3) time.Sleep(1 * time.Millisecond) // wait for propagation for i := 0; i < horizon; i++ { c, err := s.GetConn() if err != nil { t.Error(err) continue } if i == 0 { means[i].X = 0 means[i].Y = c.host.Score() } else { n := means[i-1].X + 1 m := means[i-1].Y means[i].X = float64(i) means[i].Y = m + (c.host.Score()-m)/(n+1) } a := c.Address() if err := c.Release(nil, hosts[a].trial()); err != nil { t.Error(err) continue } time.Sleep(1 * time.Millisecond) // wait for memoization } s.Close() l, err := plotter.NewLine(means) if err != nil { t.Fatal(err) } l.LineStyle.Color = color.RGBA{B: uint8(255 * e), A: 255} p.Legend.Add("epsilon "+strconv.FormatFloat(float64(e), 'f', 1, 32), l) p.Add(l) } if err := p.Save(7, 7, "egreedy_test.svg"); err != nil { t.Fatal(err) } }
// Creates a plotinum plot from the input set of points and plot labels // and saves to a file. // The plot parameters are supplied as a map of strings. The following fields are // valid: // Title - plot title (and filename) // XLabel - x axis label (default X) // YLabel - y axis label (Y) // XMin - lower bound of x axis // XMax - upper bound of x axis // YMin - lower bound of y axis // YMax - upper bound of y axis // xsize - width of the plot (in inches) (default 6 in) // ysize - height of the plot (in inches) (default 6 in) // fixaspectratio - provides fixed aspect ratio by scaling x axis (default false) func CreatePlot(pts []plotter.XYs, label []string, params map[string]string) { colors := make([]color.RGBA, 5) colors[0] = color.RGBA{R: 255, G: 0, B: 0, A: 255} colors[1] = color.RGBA{R: 0, G: 255, B: 0, A: 255} colors[2] = color.RGBA{R: 0, G: 0, B: 255, A: 255} colors[3] = color.RGBA{R: 255, G: 0, B: 255, A: 255} colors[4] = color.RGBA{R: 255, G: 255, B: 0, A: 255} p, err := plot.New() if err != nil { panic(err) } var title string if val, ok := params["Title"]; ok { title = val } p.Title.Text = title if val, ok := params["XLabel"]; ok { p.X.Label.Text = val } else { p.X.Label.Text = "X" } if val, ok := params["YLabel"]; ok { p.Y.Label.Text = val } else { p.Y.Label.Text = "Y" } if val, ok := params["XMin"]; ok { if fval, err := strconv.ParseFloat(val, 64); err == nil { p.X.Min = fval } } if val, ok := params["XMax"]; ok { if fval, err := strconv.ParseFloat(val, 64); err == nil { p.X.Max = fval } } if val, ok := params["YMin"]; ok { if fval, err := strconv.ParseFloat(val, 64); err == nil { p.Y.Min = fval } } if val, ok := params["YMax"]; ok { if fval, err := strconv.ParseFloat(val, 64); err == nil { p.Y.Max = fval } } var xsize float64 = 6.0 if val, ok := params["XSize"]; ok { if fval, err := strconv.ParseFloat(val, 64); err == nil { xsize = fval } } var ysize float64 = 6.0 if val, ok := params["YSize"]; ok { if fval, err := strconv.ParseFloat(val, 64); err == nil { ysize = fval } } var fixaspectratio bool = false if val, ok := params["FixAspectRatio"]; ok { if bval, err := strconv.ParseBool(val); err == nil { fixaspectratio = bval } } p.Add(plotter.NewGrid()) for j := 0; j < len(pts); j++ { ll, lp := plotter.NewLinePoints(pts[j]) ll.Color = colors[j%5] lp.Color = colors[j%5] p.Add(ll, lp) p.Legend.Add(label[j], ll, lp) } if fixaspectratio { if r, err := aspectRatio(p); err == nil { xsize = ysize * r } } // Save the plot to a PNG file. filename := title + ".png" if err := p.Save(xsize, ysize, filename); err != nil { panic(err) } }
// Writes step data and position to a graph func WriteStepsToChart(stepData <-chan int8) { maxNumberSteps := 1500 leftVel := make(chartplotter.XYs, maxNumberSteps) rightVel := make(chartplotter.XYs, maxNumberSteps) leftPos := make(chartplotter.XYs, maxNumberSteps) rightPos := make(chartplotter.XYs, maxNumberSteps) var byteDataL, byteDataR int8 stepIndex := 0 for stepDataOpen := true; stepDataOpen; { byteDataL, stepDataOpen = <-stepData byteDataR, stepDataOpen = <-stepData leftVel[stepIndex].X = float64(stepIndex) leftVel[stepIndex].Y = float64(byteDataL) * Settings.StepSize_MM / (32.0 * 0.002) rightVel[stepIndex].X = float64(stepIndex) rightVel[stepIndex].Y = float64(byteDataR) * Settings.StepSize_MM / (32.0 * 0.002) leftPos[stepIndex].X = float64(stepIndex) if stepIndex > 0 { leftPos[stepIndex].Y = leftPos[stepIndex-1].Y + (leftVel[stepIndex].Y * .002) } else { leftPos[stepIndex].Y = 0 } rightPos[stepIndex].X = float64(stepIndex) if stepIndex > 0 { rightPos[stepIndex].Y = rightPos[stepIndex-1].Y + (rightVel[stepIndex].Y * .002) } else { rightPos[stepIndex].Y = 0 } stepIndex++ if stepIndex >= maxNumberSteps { break } } // chop down slices if maxNumberSteps wasn't needed if stepIndex < maxNumberSteps { leftVel = leftVel[:stepIndex] rightVel = rightVel[:stepIndex] leftPos = leftPos[:stepIndex] rightPos = rightPos[:stepIndex] } // Create a new plot, set its title and // axis labels. p, err := chart.New() if err != nil { panic(err) } p.Title.Text = "Polargraph Position & Velocity" p.X.Label.Text = "2ms Slice" p.Y.Label.Text = "Position(mm) / Velocity (mm/s)" // Draw a grid behind the data p.Add(chartplotter.NewGrid()) leftPosLine, _ := chartplotter.NewLine(leftPos) leftPosLine.LineStyle.Width = vg.Points(1) leftPosLine.LineStyle.Color = color.RGBA{B: 255, A: 255} rightPosLine, _ := chartplotter.NewLine(rightPos) rightPosLine.LineStyle.Width = vg.Points(1) rightPosLine.LineStyle.Color = color.RGBA{R: 255, A: 255} leftVelLine, _ := chartplotter.NewLine(leftVel) leftVelLine.LineStyle.Width = vg.Points(1) leftVelLine.LineStyle.Color = color.RGBA{B: 150, G: 150, A: 255} rightVelLine, _ := chartplotter.NewLine(rightVel) rightVelLine.LineStyle.Width = vg.Points(1) rightVelLine.LineStyle.Color = color.RGBA{R: 150, G: 150, A: 255} p.Add(leftPosLine, rightPosLine) p.Legend.Add("Left Pos", leftPosLine) p.Legend.Add("Right Pos", rightPosLine) p.Add(leftVelLine, rightVelLine) p.Legend.Add("Left Vel", leftVelLine) p.Legend.Add("Right Vel", rightVelLine) // Save the plot to a PNG file. if err := p.Save(14, 8.5, "chart.png"); err != nil { panic(err) } }
func marshalPNG(r *http.Request, results []*metricData) []byte { p, err := plot.New() if err != nil { panic(err) } // set bg/fg colors bgcolor := string2Color(getString(r.FormValue("bgcolor"), "black")) p.BackgroundColor = bgcolor fgcolorstr := getString(r.FormValue("fgcolor"), "white") fgcolor := string2Color(fgcolorstr) p.Title.Color = fgcolor p.X.LineStyle.Color = fgcolor p.Y.LineStyle.Color = fgcolor p.X.Tick.LineStyle.Color = fgcolor p.Y.Tick.LineStyle.Color = fgcolor p.X.Tick.Label.Color = fgcolor p.Y.Tick.Label.Color = fgcolor p.X.Label.Color = fgcolor p.Y.Label.Color = fgcolor p.Legend.Color = fgcolor // set grid grid := plotter.NewGrid() grid.Vertical.Color = fgcolor grid.Horizontal.Color = fgcolor p.Add(grid) // line mode (ikruglow) TODO check values lineMode := getString(r.FormValue("lineMode"), "slope") // width and height width := getInt(r.FormValue("width"), 330) height := getInt(r.FormValue("height"), 250) // need different timeMarker's based on step size p.Title.Text = r.FormValue("title") p.X.Tick.Marker = makeTimeMarker(results[0].GetStepTime()) hideLegend := getBool(r.FormValue("hideLegend"), false) graphOnly := getBool(r.FormValue("graphOnly"), false) if graphOnly { p.HideAxes() } if len(results) == 1 && results[0].color == "" { results[0].color = fgcolorstr } var lines []plot.Plotter for i, r := range results { l := NewResponsePlotter(r) l.LineStyle.Color = fgcolor // consolidate datapoints l.maybeConsolidateData(width) if r.drawAsInfinite { l.lineMode = "drawAsInfinite" } else { l.lineMode = lineMode } if r.color != "" { l.Color = string2Color(r.color) } else { l.Color = plotutil.Color(i) } lines = append(lines, l) if !graphOnly && !hideLegend { p.Legend.Add(r.GetName(), l) } } p.Add(lines...) p.Y.Max *= 1.05 p.Y.Min *= 0.95 // Draw the plot to an in-memory image. img := image.NewRGBA(image.Rect(0, 0, width, height)) da := plot.MakeDrawArea(vgimg.NewImage(img)) p.Draw(da) var b bytes.Buffer if err := png.Encode(&b, img); err != nil { panic(err) } return b.Bytes() }