...

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

Documentation: gonum.org/v1/plot/plotter

     1  // Copyright ©2020 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  	"os"
    10  
    11  	"gonum.org/v1/plot"
    12  	"gonum.org/v1/plot/plotter"
    13  	"gonum.org/v1/plot/vg"
    14  	"gonum.org/v1/plot/vg/draw"
    15  	"gonum.org/v1/plot/vg/vgimg"
    16  )
    17  
    18  func ExampleLabels() {
    19  	p := plot.New()
    20  	p.Title.Text = "Labels"
    21  	p.X.Min = -10
    22  	p.X.Max = +10
    23  	p.Y.Min = 0
    24  	p.Y.Max = +20
    25  
    26  	labels, err := plotter.NewLabels(plotter.XYLabels{
    27  		XYs: []plotter.XY{
    28  			{X: -5, Y: 5},
    29  			{X: +5, Y: 5},
    30  			{X: +5, Y: 15},
    31  			{X: -5, Y: 15},
    32  		},
    33  		Labels: []string{"A", "B", "C", "D"},
    34  	})
    35  	if err != nil {
    36  		log.Fatalf("could not creates labels plotter: %+v", err)
    37  	}
    38  
    39  	p.Add(labels)
    40  
    41  	err = p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/labels.png")
    42  	if err != nil {
    43  		log.Fatalf("could save plot: %+v", err)
    44  	}
    45  }
    46  
    47  // ExampleLabels_inCanvasCoordinates shows how to write a label
    48  // in a plot using the canvas coordinates (instead of the data coordinates.)
    49  // It can be useful in the situation where one wants to draw some label
    50  // always in the same location of a plot, irrespective of the minute
    51  // details of a particular plot data range.
    52  func ExampleLabels_inCanvasCoordinates() {
    53  	p := plot.New()
    54  	p.Title.Text = "Labels - X"
    55  	p.X.Min = -10
    56  	p.X.Max = +10
    57  	p.Y.Min = 0
    58  	p.Y.Max = +20
    59  
    60  	labels, err := plotter.NewLabels(plotter.XYLabels{
    61  		XYs: []plotter.XY{
    62  			{X: -5, Y: 5},
    63  			{X: +5, Y: 5},
    64  			{X: +5, Y: 15},
    65  			{X: -5, Y: 15},
    66  		},
    67  		Labels: []string{"A", "B", "C", "D"},
    68  	},
    69  	)
    70  	if err != nil {
    71  		log.Fatalf("could not creates labels plotter: %+v", err)
    72  	}
    73  
    74  	p.Add(labels)
    75  
    76  	f, err := os.Create("testdata/labels_cnv_coords.png")
    77  	if err != nil {
    78  		log.Fatalf("could not create output plot file: %+v", err)
    79  	}
    80  	defer f.Close()
    81  
    82  	cnv := vgimg.PngCanvas{
    83  		Canvas: vgimg.New(10*vg.Centimeter, 10*vg.Centimeter),
    84  	}
    85  
    86  	dc := draw.New(cnv)
    87  	p.Draw(dc)
    88  
    89  	// Put an 'X' in the middle of the data-canvas.
    90  	{
    91  		fnt := p.TextHandler.Cache().Lookup(plotter.DefaultFont, vg.Points(12))
    92  		da := p.DataCanvas(dc)
    93  		da.FillString(fnt, vg.Point{X: da.X(0.5), Y: da.Y(0.5)}, "X")
    94  	}
    95  
    96  	_, err = cnv.WriteTo(f)
    97  	if err != nil {
    98  		log.Fatalf("could not write to output plot file: %+v", err)
    99  	}
   100  
   101  	err = f.Close()
   102  	if err != nil {
   103  		log.Fatalf("could save plot: %+v", err)
   104  	}
   105  }
   106  

View as plain text