// 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 := plotter.NewLine(pts) scatter := plotter.NewScatter(pts) p.Add(line, scatter) pts = plotter.XYs{{1, 0}, {0.75, 0}, {0.75, 0.75}} line = plotter.NewLine(pts) scatter = plotter.NewScatter(pts) p.Add(line, scatter) pts = plotter.XYs{{0.5, 0.5}, {1, 0.5}} line = plotter.NewLine(pts) scatter = plotter.NewScatter(pts) p.Add(line, scatter) return p }
// 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 := plotter.NewHist(vals, 16) 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 }
// 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{ plotter.XYs: randomPoints(n), plotter.YErrors: plotter.YErrors(randomError(n)), plotter.XErrors: plotter.XErrors(randomError(n)), } p, err := plot.New() if err != nil { panic(err) } scatter := plotter.NewScatter(data) scatter.Shape = plot.CrossGlyph{} p.Add(scatter, plotter.NewXErrorBars(data), plotter.NewYErrorBars(data)) p.Add(plotter.NewGlyphBoxes()) return p }
// 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 }
// 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 }
func ExampleErrorPoints() { // 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 := NewErrorPoints(MeanAndConf95, pts...) medMinMax := NewErrorPoints(MedianAndMinMax, pts...) AddLinePoints(plt, "mean and 95% confidence", mean95, "median and minimum and maximum", medMinMax) AddErrorBars(plt, mean95, medMinMax) AddScatters(plt, pts[0], pts[1], pts[2], pts[3], pts[4]) plt.Save(4, 4, "centroids.png") }
// Example_horizontalBoxPlots draws horizontal boxplots // with some labels on their points. func Example_horizontalBoxPlots() *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 Box Plot" p.X.Label.Text = "plotter.Values" // Make boxes for our data and add them to the plot. uniBox := plotter.HorizBoxPlot{plotter.NewBoxPlot(vg.Points(20), 0, uniform)} uniLabels, err := uniBox.OutsideLabels(uniform) if err != nil { panic(err) } normBox := plotter.HorizBoxPlot{plotter.NewBoxPlot(vg.Points(20), 1, normal)} normLabels, err := normBox.OutsideLabels(normal) if err != nil { panic(err) } expBox := plotter.HorizBoxPlot{plotter.NewBoxPlot(vg.Points(20), 2, expon)} 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 }
// An example of making a bar chart. func Example_barChart() *plot.Plot { groupA := plotter.Values{20, 35, 30, 35, 27} groupB := plotter.Values{25, 32, 34, 20, 25} groupC := plotter.Values{12, 28, 15, 21, 8} groupD := plotter.Values{30, 42, 6, 9, 12} p, err := plot.New() if err != nil { panic(err) } p.Title.Text = "Bar chart" p.Y.Label.Text = "Heights" w := vg.Points(8) barsA := plotter.NewBarChart(groupA, w) barsA.Color = color.RGBA{R: 255, A: 255} barsA.Offset = -w / 2 barsB := plotter.NewBarChart(groupB, w) barsB.Color = color.RGBA{R: 196, G: 196, A: 255} barsB.Offset = w / 2 barsC := plotter.NewBarChart(groupC, w) barsC.Color = color.RGBA{B: 255, A: 255} barsC.XMin = 6 barsC.Offset = -w / 2 barsD := plotter.NewBarChart(groupD, w) barsD.Color = color.RGBA{B: 255, R: 255, A: 255} barsD.XMin = 6 barsD.Offset = w / 2 p.Add(barsA, barsB, barsC, barsD) p.Legend.Add("A", barsA) p.Legend.Add("B", barsB) p.Legend.Add("C", barsC) p.Legend.Add("D", barsD) p.Legend.Top = true p.NominalX("Zero", "One", "Two", "Three", "Four", "", "Six", "Seven", "Eight", "Nine", "Ten") return p }
func Example_bubbles() *plot.Plot { rand.Seed(int64(0)) n := 10 bubbleData := randomTriples(n) p, err := plot.New() if err != nil { panic(err) } p.Title.Text = "Bubbles" p.X.Label.Text = "X" p.Y.Label.Text = "Y" bs := plotter.NewBubbles(bubbleData, vg.Points(1), vg.Points(20)) bs.Color = color.RGBA{R: 196, B: 128, A: 255} p.Add(bs) return p }
// 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 := plotter.NewScatter(scatterData) s.GlyphStyle.Color = color.RGBA{R: 255, B: 128, A: 255} s.GlyphStyle.Radius = vg.Points(3) l := plotter.NewLine(lineData) 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 := plotter.NewLinePoints(linePointsData) 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 }