...

Source file src/gonum.org/v1/plot/plotter/quartile_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  	"log"
     9  
    10  	"golang.org/x/exp/rand"
    11  
    12  	"gonum.org/v1/plot"
    13  	"gonum.org/v1/plot/plotter"
    14  	"gonum.org/v1/plot/vg"
    15  )
    16  
    17  func ExampleQuartPlot() {
    18  	rnd := rand.New(rand.NewSource(1))
    19  
    20  	// Create the example data.
    21  	n := 100
    22  	uniform := make(plotter.Values, n)
    23  	normal := make(plotter.Values, n)
    24  	expon := make(plotter.Values, n)
    25  	for i := 0; i < n; i++ {
    26  		uniform[i] = rnd.Float64()
    27  		normal[i] = rnd.NormFloat64()
    28  		expon[i] = rnd.ExpFloat64()
    29  	}
    30  
    31  	// Create the QuartPlots
    32  	qp1, err := plotter.NewQuartPlot(0, uniform)
    33  	if err != nil {
    34  		log.Panic(err)
    35  	}
    36  	qp2, err := plotter.NewQuartPlot(1, normal)
    37  	if err != nil {
    38  		log.Panic(err)
    39  	}
    40  	qp3, err := plotter.NewQuartPlot(2, expon)
    41  	if err != nil {
    42  		log.Panic(err)
    43  	}
    44  
    45  	// Create a vertical plot
    46  	p1 := plot.New()
    47  	p1.Title.Text = "Quartile Plot"
    48  	p1.Y.Label.Text = "plotter.Values"
    49  	p1.Add(qp1, qp2, qp3)
    50  
    51  	// Set the X axis of the plot to nominal with
    52  	// the given names for x=0, x=1 and x=2.
    53  	p1.NominalX("Uniform\nDistribution", "Normal\nDistribution",
    54  		"Exponential\nDistribution")
    55  
    56  	err = p1.Save(200, 200, "testdata/verticalQuartPlot.png")
    57  	if err != nil {
    58  		log.Panic(err)
    59  	}
    60  
    61  	// Create a horizontal plot
    62  	qp1.Horizontal = true
    63  	qp2.Horizontal = true
    64  	qp3.Horizontal = true
    65  
    66  	p2 := plot.New()
    67  	p2.Title.Text = "Quartile Plot"
    68  	p2.X.Label.Text = "plotter.Values"
    69  	p2.Add(qp1, qp2, qp3)
    70  
    71  	// Set the Y axis of the plot to nominal with
    72  	// the given names for y=0, y=1 and y=2.
    73  	p2.NominalY("Uniform\nDistribution", "Normal\nDistribution",
    74  		"Exponential\nDistribution")
    75  
    76  	err = p2.Save(200, 200, "testdata/horizontalQuartPlot.png")
    77  	if err != nil {
    78  		log.Panic(err)
    79  	}
    80  
    81  	// Now, create a grouped quartile plot.
    82  
    83  	p3 := plot.New()
    84  	p3.Title.Text = "Box Plot"
    85  	p3.Y.Label.Text = "plotter.Values"
    86  
    87  	w := vg.Points(10)
    88  	for x := 0.0; x < 3.0; x++ {
    89  		b0, err := plotter.NewQuartPlot(x, uniform)
    90  		if err != nil {
    91  			log.Panic(err)
    92  		}
    93  		b0.Offset = -w
    94  		b1, err := plotter.NewQuartPlot(x, normal)
    95  		if err != nil {
    96  			log.Panic(err)
    97  		}
    98  		b2, err := plotter.NewQuartPlot(x, expon)
    99  		if err != nil {
   100  			log.Panic(err)
   101  		}
   102  		b2.Offset = w
   103  		p3.Add(b0, b1, b2)
   104  	}
   105  	p3.Add(plotter.NewGlyphBoxes())
   106  
   107  	p3.NominalX("Group 0", "Group 1", "Group 2")
   108  
   109  	err = p3.Save(200, 200, "testdata/groupedQuartPlot.png")
   110  	if err != nil {
   111  		log.Panic(err)
   112  	}
   113  }
   114  

View as plain text