...

Source file src/gonum.org/v1/plot/align_example_test.go

Documentation: gonum.org/v1/plot

     1  // Copyright ©2017 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 plot_test
     6  
     7  import (
     8  	"log"
     9  	"math"
    10  	"os"
    11  
    12  	"gonum.org/v1/plot"
    13  	"gonum.org/v1/plot/vg"
    14  	"gonum.org/v1/plot/vg/draw"
    15  	"gonum.org/v1/plot/vg/vgimg"
    16  )
    17  
    18  func ExampleAlign() {
    19  	const rows, cols = 4, 3
    20  	plots := make([][]*plot.Plot, rows)
    21  	for j := 0; j < rows; j++ {
    22  		plots[j] = make([]*plot.Plot, cols)
    23  		for i := 0; i < cols; i++ {
    24  			if i == 0 && j == 2 {
    25  				// This shows what happens when there are nil plots.
    26  				continue
    27  			}
    28  
    29  			p := plot.New()
    30  
    31  			if j == 0 && i == 2 {
    32  				// This shows what happens when the axis padding
    33  				// is different among plots.
    34  				p.X.Padding, p.Y.Padding = 0, 0
    35  			}
    36  
    37  			if j == 1 && i == 1 {
    38  				// To test the Align function, we make the axis labels
    39  				// on one of the plots stick out.
    40  				p.Y.Max = 1e9
    41  				p.X.Max = 1e9
    42  				p.X.Tick.Label.Rotation = math.Pi / 2
    43  				p.X.Tick.Label.XAlign = draw.XRight
    44  				p.X.Tick.Label.YAlign = draw.YCenter
    45  				p.X.Tick.Label.Font.Size = 8
    46  				p.Y.Tick.Label.Font.Size = 8
    47  			} else {
    48  				p.Y.Max = 1e9
    49  				p.X.Max = 1e9
    50  				p.X.Tick.Label.Font.Size = 1
    51  				p.Y.Tick.Label.Font.Size = 1
    52  			}
    53  
    54  			plots[j][i] = p
    55  		}
    56  	}
    57  
    58  	img := vgimg.New(vg.Points(150), vg.Points(175))
    59  	dc := draw.New(img)
    60  
    61  	t := draw.Tiles{
    62  		Rows:      rows,
    63  		Cols:      cols,
    64  		PadX:      vg.Millimeter,
    65  		PadY:      vg.Millimeter,
    66  		PadTop:    vg.Points(2),
    67  		PadBottom: vg.Points(2),
    68  		PadLeft:   vg.Points(2),
    69  		PadRight:  vg.Points(2),
    70  	}
    71  
    72  	canvases := plot.Align(plots, t, dc)
    73  	for j := 0; j < rows; j++ {
    74  		for i := 0; i < cols; i++ {
    75  			if plots[j][i] != nil {
    76  				plots[j][i].Draw(canvases[j][i])
    77  			}
    78  		}
    79  	}
    80  
    81  	w, err := os.Create("testdata/align.png")
    82  	if err != nil {
    83  		panic(err)
    84  	}
    85  	defer w.Close()
    86  	png := vgimg.PngCanvas{Canvas: img}
    87  	if _, err := png.WriteTo(w); err != nil {
    88  		panic(err)
    89  	}
    90  }
    91  
    92  func ExampleAxis_labelsPosition() {
    93  	p := plot.New()
    94  	p.Title.Text = "Title"
    95  	p.X.Label.Text = "X [mm]"
    96  	p.Y.Label.Text = "Y [A.U.]"
    97  	p.X.Label.Position = draw.PosRight
    98  	p.Y.Label.Position = draw.PosTop
    99  	p.X.Min = -10
   100  	p.X.Max = +10
   101  	p.Y.Min = -10
   102  	p.Y.Max = +10
   103  
   104  	err := p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/axis_labels.png")
   105  	if err != nil {
   106  		log.Fatalf("could not save plot: %+v", err)
   107  	}
   108  }
   109  

View as plain text