MCPcopy
hub / github.com/gonum/plot / ExampleLabels_inCanvasCoordinates

Function ExampleLabels_inCanvasCoordinates

plotter/labels_example_test.go:52–105  ·  view source on GitHub ↗

ExampleLabels_inCanvasCoordinates shows how to write a label in a plot using the canvas coordinates (instead of the data coordinates.) It can be useful in the situation where one wants to draw some label always in the same location of a plot, irrespective of the minute details of a particular plot d

()

Source from the content-addressed store, hash-verified

50// always in the same location of a plot, irrespective of the minute
51// details of a particular plot data range.
52func 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}

Callers

nothing calls this directly

Calls 15

WriteToMethod · 0.95
NewFunction · 0.92
NewLabelsFunction · 0.92
NewFunction · 0.92
NewFunction · 0.92
PointsFunction · 0.92
CloseMethod · 0.80
LookupMethod · 0.80
DataCanvasMethod · 0.80
DrawMethod · 0.65
CacheMethod · 0.65
FillStringMethod · 0.65

Tested by

no test coverage detected