...

Source file src/gonum.org/v1/plot/plotter/boxplot_example_test.go

Documentation: gonum.org/v1/plot/plotter

     1  // Copyright ©2015 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package plotter_test
     6  
     7  import (
     8  	"fmt"
     9  	"image/color"
    10  	"log"
    11  
    12  	"golang.org/x/exp/rand"
    13  
    14  	"gonum.org/v1/plot"
    15  	"gonum.org/v1/plot/plotter"
    16  	"gonum.org/v1/plot/vg"
    17  )
    18  
    19  func ExampleBoxPlot() {
    20  	rnd := rand.New(rand.NewSource(1))
    21  
    22  	// Create the sample data.
    23  	const n = 100
    24  	uniform := make(plotter.ValueLabels, n)
    25  	normal := make(plotter.ValueLabels, n)
    26  	expon := make(plotter.ValueLabels, n)
    27  	for i := 0; i < n; i++ {
    28  		uniform[i].Value = rnd.Float64()
    29  		uniform[i].Label = fmt.Sprintf("%4.4f", uniform[i].Value)
    30  		normal[i].Value = rnd.NormFloat64()
    31  		normal[i].Label = fmt.Sprintf("%4.4f", normal[i].Value)
    32  		expon[i].Value = rnd.ExpFloat64()
    33  		expon[i].Label = fmt.Sprintf("%4.4f", expon[i].Value)
    34  	}
    35  
    36  	// Make boxes for our data and add them to the plot.
    37  	uniBox, err := plotter.NewBoxPlot(vg.Points(20), 0, uniform)
    38  	if err != nil {
    39  		log.Panic(err)
    40  	}
    41  	uniBox.FillColor = color.RGBA{127, 188, 165, 1}
    42  	normBox, err := plotter.NewBoxPlot(vg.Points(20), 1, normal)
    43  	if err != nil {
    44  		log.Panic(err)
    45  	}
    46  	normBox.FillColor = color.RGBA{127, 188, 165, 1}
    47  	expBox, err := plotter.NewBoxPlot(vg.Points(20), 2, expon)
    48  	if err != nil {
    49  		log.Panic(err)
    50  	}
    51  	expBox.FillColor = color.RGBA{127, 188, 165, 1}
    52  
    53  	// Make a vertical box plot.
    54  	uniLabels, err := uniBox.OutsideLabels(uniform)
    55  	if err != nil {
    56  		log.Panic(err)
    57  	}
    58  	normLabels, err := normBox.OutsideLabels(normal)
    59  	if err != nil {
    60  		log.Panic(err)
    61  	}
    62  	expLabels, err := expBox.OutsideLabels(expon)
    63  	if err != nil {
    64  		log.Panic(err)
    65  	}
    66  
    67  	p1 := plot.New()
    68  	p1.Title.Text = "Vertical Box Plot"
    69  	p1.Y.Label.Text = "plotter.Values"
    70  	p1.Y.Max = 6
    71  	p1.Add(uniBox, uniLabels, normBox, normLabels, expBox, expLabels)
    72  
    73  	// Set the X axis of the plot to nominal with
    74  	// the given names for x=0, x=1 and x=2.
    75  	p1.NominalX("Uniform\nDistribution", "Normal\nDistribution",
    76  		"Exponential\nDistribution")
    77  
    78  	err = p1.Save(200, 200, "testdata/verticalBoxPlot.png")
    79  	if err != nil {
    80  		log.Panic(err)
    81  	}
    82  
    83  	// Now, make the same plot but horizontal.
    84  	normBox.Horizontal = true
    85  	expBox.Horizontal = true
    86  	uniBox.Horizontal = true
    87  	// We can use the same plotters but the labels need to be recreated.
    88  	uniLabels, err = uniBox.OutsideLabels(uniform)
    89  	if err != nil {
    90  		log.Panic(err)
    91  	}
    92  	normLabels, err = normBox.OutsideLabels(normal)
    93  	if err != nil {
    94  		log.Panic(err)
    95  	}
    96  	expLabels, err = expBox.OutsideLabels(expon)
    97  	if err != nil {
    98  		log.Panic(err)
    99  	}
   100  
   101  	p2 := plot.New()
   102  	p2.Title.Text = "Horizontal Box Plot"
   103  	p2.X.Label.Text = "plotter.Values"
   104  
   105  	p2.Add(uniBox, uniLabels, normBox, normLabels, expBox, expLabels)
   106  
   107  	// Set the Y axis of the plot to nominal with
   108  	// the given names for y=0, y=1 and y=2.
   109  	p2.NominalY("Uniform\nDistribution", "Normal\nDistribution",
   110  		"Exponential\nDistribution")
   111  
   112  	err = p2.Save(200, 200, "testdata/horizontalBoxPlot.png")
   113  	if err != nil {
   114  		log.Panic(err)
   115  	}
   116  
   117  	// Now, make a grouped box plot.
   118  	p3 := plot.New()
   119  	p3.Title.Text = "Box Plot"
   120  	p3.Y.Label.Text = "plotter.Values"
   121  
   122  	w := vg.Points(20)
   123  	for x := 0.0; x < 3.0; x++ {
   124  		b0, err := plotter.NewBoxPlot(w, x, uniform)
   125  		if err != nil {
   126  			log.Panic(err)
   127  		}
   128  		b0.Offset = -w - vg.Points(3)
   129  		b1, err := plotter.NewBoxPlot(w, x, normal)
   130  		if err != nil {
   131  			log.Panic(err)
   132  		}
   133  		b2, err := plotter.NewBoxPlot(w, x, expon)
   134  		if err != nil {
   135  			log.Panic(err)
   136  		}
   137  		b2.Offset = w + vg.Points(3)
   138  		p3.Add(b0, b1, b2)
   139  	}
   140  	// Add a GlyphBox plotter for debugging.
   141  	p3.Add(plotter.NewGlyphBoxes())
   142  
   143  	// Set the X axis of the plot to nominal with
   144  	// the given names for x=0, x=1 and x=2.
   145  	p3.NominalX("Group 0", "Group 1", "Group 2")
   146  	err = p3.Save(300, 300, "testdata/groupedBoxPlot.png")
   147  	if err != nil {
   148  		log.Panic(err)
   149  	}
   150  }
   151  

View as plain text