Esempio n. 1
0
// AddLinePoints adds Line and Scatter plotters to a
// plot.  The variadic arguments must be either strings
// or plotter.XYers.  Each plotter.XYer is added to
// the plot using the next color, dashes, and glyph
// shape via the Color, Dashes, and Shape functions.
// If a plotter.XYer is immediately preceeded by
// a string then a legend entry is added to the plot
// using the string as the name.
func AddLinePoints(plt *plot.Plot, vs ...interface{}) {
	name := ""
	var i int
	for _, v := range vs {
		switch t := v.(type) {
		case string:
			name = t

		case plotter.XYer:
			l, s := plotter.NewLinePoints(t)
			l.Color = Color(i)
			l.Dashes = Dashes(i)
			s.Color = Color(i)
			s.Shape = Shape(i)
			i++
			plt.Add(l, s)
			if name != "" {
				plt.Legend.Add(name, l, s)
				name = ""
			}

		default:
			panic(fmt.Sprintf("AddLinePoints handles strings and plotter.XYers, got %T", t))
		}
	}
}
Esempio n. 2
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
}
Esempio n. 3
0
// 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
}
Esempio n. 4
0
// AddLinePoints adds Line and Scatter plotters to a
// plot.  The variadic arguments must be either strings
// or plotter.XYers.  Each plotter.XYer is added to
// the plot using the next color, dashes, and glyph
// shape via the Color, Dashes, and Shape functions.
// If a plotter.XYer is immediately preceeded by
// a string then a legend entry is added to the plot
// using the string as the name.
//
// If an error occurs then none of the plotters are added
// to the plot, and the error is returned.
func AddLinePoints(plt *plot.Plot, vs ...interface{}) error {
	var ps []plot.Plotter
	names := make(map[[2]plot.Thumbnailer]string)
	name := ""
	var i int
	for _, v := range vs {
		switch t := v.(type) {
		case string:
			name = t

		case plotter.XYer:
			l, s, err := plotter.NewLinePoints(t)
			if err != nil {
				return err
			}
			l.Color = Color(i)
			l.Dashes = Dashes(i)
			s.Color = Color(i)
			s.Shape = Shape(i)
			i++
			ps = append(ps, l, s)
			if name != "" {
				names[[2]plot.Thumbnailer{l, s}] = name
				name = ""
			}

		default:
			panic(fmt.Sprintf("AddLinePoints handles strings and plotter.XYers, got %T", t))
		}
	}
	plt.Add(ps...)
	for ps, n := range names {
		plt.Legend.Add(n, ps[0], ps[1])
	}
	return nil
}
Esempio n. 5
0
func main() {
	fmt.Println("Running...")
	start := time.Now()

	y1 := make([]float64, 0)
	y2 := make([]float64, 0)

	file, errF := os.Open(fileName)

	if errF != nil {
		fmt.Println("Error:", errF)
		return
	}

	reader := csv.NewReader(file)
	reader.TrimLeadingSpace = true
	for {
		record, err := reader.Read()
		if err == io.EOF {
			break
		} else if err != nil {
			fmt.Println("Error:", err)
			return
		}
		ft1, err1 := strconv.ParseFloat(record[1], 64)
		ft2, err2 := strconv.ParseFloat(record[2], 64)
		if err1 != nil {
			fmt.Println("Error:", err1)
		}
		if err2 != nil {
			fmt.Println("Error:", err2)
		}
		y1 = append(y1, ft1)
		y2 = append(y2, ft2)
		//fmt.Println(record[1],record[2])
		//fmt.Println(err1,err2)
	}

	var pt plotter.XYs
	pt = make(plotter.XYs, len(y1))

	for i := range pt {
		pt[i].X = y1[i]
		pt[i].Y = y2[i]
	}

	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = fmt.Sprintf("Van Der Pol Oscillator")
	p.Y.Label.Text = "y2(t)"
	p.X.Label.Text = "y1(t)"

	/*
	   p.X.Min = 0
	   p.X.Max = 4.5
	   p.Y.Min = 0
	   p.Y.Max = 4.5
	*/
	lpLine, lpPoints := plotter.NewLinePoints(pt)

	lpLine.LineStyle.Width = vg.Points(0)
	lpLine.Color = color.RGBA{G: 255, A: 255}
	lpPoints.Shape = plot.CircleGlyph{}
	lpPoints.Color = color.RGBA{R: 255, A: 255}
	lpPoints.GlyphStyle.Radius = vg.Points(2.0)
	p.Add(lpPoints)

	//plotutil.AddLinePoints(p,pt)
	//plotutil.AddLinePoints(p,pt)

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

	fmt.Println(time.Since(start))
	file.Close()
	fmt.Println("...program terminated successfully!")
}
Esempio n. 6
0
// 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)
	}
}
Esempio n. 7
0
func VisualizeSignificantEvents(events SignificantEvents, filename string, options SignificantEventsOptions) {
	firstTime := events.FirstTime()
	tr := func(t time.Time) float64 {
		return t.Sub(firstTime).Seconds()
	}

	minX := 0.0
	if options.MinX != 0 {
		minX = options.MinX
	}
	if !options.MinT.IsZero() {
		minX = tr(options.MinT)
	}
	maxX := 0.0
	maxY := 0.0
	minY := math.MaxFloat64

	scatters := map[string][]plot.Plotter{}
	verticalLines := []plot.Plotter{}
	lineOverlays := []plot.Plotter{}

	colorCounter := 0
	for _, name := range events.OrderedNames() {
		xys := plotter.XYs{}
		xErrs := plotter.XErrors{}
		for _, event := range events[name] {
			if event.V > 0 {
				xys = append(xys, struct{ X, Y float64 }{tr(event.T), event.V})
				xErrs = append(xErrs, struct{ Low, High float64 }{-event.V / 2, event.V / 2})

				if tr(event.T) > maxX {
					maxX = tr(event.T)
				}
				if event.V > maxY {
					maxY = event.V
				}
				if event.V < minY {
					minY = event.V
				}
			}
		}

		if len(xys) == 0 {
			say.Println(0, "No data for %s", name)
			continue
		}

		if options.MarkedEvents != nil {
			ls, ok := options.MarkedEvents[name]
			if ok {
				for _, event := range events[name] {
					l := viz.NewVerticalLine(tr(event.T))
					l.LineStyle = ls
					verticalLines = append(verticalLines, l)
				}
			}
		}

		s, err := plotter.NewScatter(xys)
		say.ExitIfError("Couldn't create scatter plot", err)

		s.GlyphStyle = plot.GlyphStyle{
			Color:  viz.OrderedColor(colorCounter),
			Radius: 2,
			Shape:  plot.CircleGlyph{},
		}

		xErrsPlot, err := plotter.NewXErrorBars(struct {
			plotter.XYer
			plotter.XErrorer
		}{xys, xErrs})
		say.ExitIfError("Couldn't create x errors plot", err)
		xErrsPlot.LineStyle = viz.LineStyle(viz.OrderedColor(colorCounter), 1)

		scatters[name] = []plot.Plotter{s, xErrsPlot}

		colorCounter++
	}

	for _, marker := range options.VerticalMarkers {
		l := viz.NewVerticalLine(tr(marker.T))
		l.LineStyle = marker.LineStyle
		verticalLines = append(verticalLines, l)
	}

	for _, lineOverlay := range options.LineOverlays {
		xys := plotter.XYs{}
		for _, event := range lineOverlay.Events {
			if event.V > 0 {
				xys = append(xys, struct{ X, Y float64 }{tr(event.T), event.V})
			}
		}

		l, s, err := plotter.NewLinePoints(xys)
		say.ExitIfError("Couldn't create scatter plot", err)

		l.LineStyle = lineOverlay.LineStyle
		s.GlyphStyle = plot.GlyphStyle{
			Color:  lineOverlay.LineStyle.Color,
			Radius: lineOverlay.LineStyle.Width,
			Shape:  plot.CrossGlyph{},
		}
		lineOverlays = append(lineOverlays, l, s)
	}

	if options.MaxX != 0 {
		maxX = options.MaxX
	}
	if !options.MaxT.IsZero() {
		maxX = tr(options.MaxT)
	}

	maxY = math.Pow(10, math.Ceil(math.Log10(maxY)))

	if options.MaxY != 0 {
		maxY = options.MaxY
	}

	minY = math.Pow(10, math.Floor(math.Log10(minY)))

	n := len(scatters) + 1
	b := viz.NewUniformBoard(1, n, 0.01)

	allScatterPlot, _ := plot.New()
	allScatterPlot.Title.Text = "All Events"
	allScatterPlot.X.Label.Text = "Time (s)"
	allScatterPlot.Y.Label.Text = "Duration (s)"
	allScatterPlot.Y.Scale = plot.LogScale
	allScatterPlot.Y.Tick.Marker = plot.LogTicks

	for i, name := range events.OrderedNames() {
		scatter, ok := scatters[name]
		if !ok {
			continue
		}

		allScatterPlot.Add(scatter[0])
		allScatterPlot.Add(scatter[1])

		scatterPlot, _ := plot.New()
		scatterPlot.Title.Text = name
		scatterPlot.X.Label.Text = "Time (s)"
		scatterPlot.Y.Label.Text = "Duration (s)"
		scatterPlot.Y.Scale = plot.LogScale
		scatterPlot.Y.Tick.Marker = plot.LogTicks
		scatterPlot.Add(scatter...)
		scatterPlot.Add(verticalLines...)
		scatterPlot.Add(lineOverlays...)
		scatterPlot.Add(options.OverlayPlots...)
		scatterPlot.X.Min = minX
		scatterPlot.X.Max = maxX
		scatterPlot.Y.Min = 1e-5
		scatterPlot.Y.Max = maxY

		b.AddSubPlotAt(scatterPlot, 0, n-2-i)
	}

	allScatterPlot.Add(verticalLines...)
	allScatterPlot.Add(lineOverlays...)
	allScatterPlot.Add(options.OverlayPlots...)
	allScatterPlot.X.Min = minX
	allScatterPlot.X.Max = maxX
	allScatterPlot.Y.Min = 1e-5
	allScatterPlot.Y.Max = maxY
	fmt.Println("all", minX, maxX)

	b.AddSubPlotAt(allScatterPlot, 0, n-1)

	width := 12.0
	if options.WidthStretch > 0 {
		width = width * options.WidthStretch
	}
	b.Save(width, 5*float64(n), filename)
}