go-hep.org/x/hep@v0.38.1/hplot/label_example_test.go (about)

     1  // Copyright ©2020 The go-hep 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 hplot_test
     6  
     7  import (
     8  	"log"
     9  
    10  	xfnt "golang.org/x/image/font"
    11  	"gonum.org/v1/plot/font"
    12  	"gonum.org/v1/plot/plotter"
    13  	"gonum.org/v1/plot/plotutil"
    14  	"gonum.org/v1/plot/text"
    15  	"gonum.org/v1/plot/vg"
    16  
    17  	"go-hep.org/x/hep/hplot"
    18  )
    19  
    20  func ExampleLabel() {
    21  
    22  	// Creating a new plot
    23  	p := hplot.New()
    24  	p.Title.Text = "Plot labels"
    25  	p.X.Min = -10
    26  	p.X.Max = +10
    27  	p.Y.Min = -10
    28  	p.Y.Max = +10
    29  
    30  	// Default labels
    31  	l1 := hplot.NewLabel(-8, 5, "(-8,5)\nDefault label")
    32  	p.Add(l1)
    33  
    34  	// Label with normalized coordinates.
    35  	l3 := hplot.NewLabel(
    36  		0.5, 0.5,
    37  		"(0.5,0.5)\nLabel with relative coords",
    38  		hplot.WithLabelNormalized(true),
    39  	)
    40  	p.Add(l3)
    41  
    42  	// Label with normalized coordinates and auto-adjustement.
    43  	l4 := hplot.NewLabel(
    44  		0.95, 0.95,
    45  		"(0.95,0.95)\nLabel at the canvas edge, with AutoAdjust",
    46  		hplot.WithLabelNormalized(true),
    47  		hplot.WithLabelAutoAdjust(true),
    48  	)
    49  	p.Add(l4)
    50  
    51  	// Label with a customed TextStyle
    52  	usrFont := font.Font{
    53  		Typeface: "Liberation",
    54  		Variant:  "Mono",
    55  		Weight:   xfnt.WeightBold,
    56  		Style:    xfnt.StyleNormal,
    57  		Size:     12,
    58  	}
    59  	sty := text.Style{
    60  		Color: plotutil.Color(2),
    61  		Font:  usrFont,
    62  	}
    63  	l5 := hplot.NewLabel(
    64  		0.0, 0.1,
    65  		"(0.0,0.1)\nLabel with a user-defined font",
    66  		hplot.WithLabelTextStyle(sty),
    67  		hplot.WithLabelNormalized(true),
    68  	)
    69  	p.Add(l5)
    70  
    71  	p.Add(plotter.NewGlyphBoxes())
    72  	p.Add(hplot.NewGrid())
    73  
    74  	// Save the plot to a PNG file.
    75  	err := p.Save(15*vg.Centimeter, -1, "testdata/label_plot.png")
    76  	if err != nil {
    77  		log.Fatalf("error saving plot: %v\n", err)
    78  	}
    79  }