// 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_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 }
// 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. func AddBoxPlots(plt *plot.Plot, width vg.Length, vs ...interface{}) { var names []string name := "" for _, v := range vs { switch t := v.(type) { case string: name = t case plotter.Valuer: plt.Add(plotter.NewBoxPlot(width, float64(len(names)), t)) names = append(names, name) name = "" default: panic(fmt.Sprintf("AddScatters handles strings and plotter.XYers, got %T", t)) } } plt.NominalX(names...) }