// 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(must(plotter.NewBoxPlot(vg.Points(20), 0, uniform)).(*plotter.BoxPlot), must(plotter.NewBoxPlot(vg.Points(20), 1, normal)).(*plotter.BoxPlot), must(plotter.NewBoxPlot(vg.Points(20), 2, expon)).(*plotter.BoxPlot)) // 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_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 }
// 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 }
// AddBoxPlots adds box plot plotters to a plot and // sets the X axis of the plot to be nominal. // The variadic arguments must be either strings // or plotter.Valuers. Each valuer adds a box plot // to the plot at the X location corresponding to // the number of box plots added before it. If a // plotter.Valuer is immediately preceeded by a // string then the string value is used to label the // tick mark for the box plot's X location. // // If an error occurs then none of the plotters are added // to the plot, and the error is returned. func AddBoxPlots(plt *plot.Plot, width vg.Length, vs ...interface{}) error { var ps []plot.Plotter var names []string name := "" for _, v := range vs { switch t := v.(type) { case string: name = t case plotter.Valuer: b, err := plotter.NewBoxPlot(width, float64(len(names)), t) if err != nil { return err } ps = append(ps, b) names = append(names, name) name = "" default: panic(fmt.Sprintf("AddBoxPlots handles strings and plotter.Valuers, got %T", t)) } } plt.Add(ps...) plt.NominalX(names...) return nil }