...
1
2
3
4
5 package plotter_test
6
7 import (
8 "image/color"
9 "log"
10 "math"
11
12 "gonum.org/v1/plot"
13 "gonum.org/v1/plot/plotter"
14 "gonum.org/v1/plot/vg"
15 "gonum.org/v1/plot/vg/draw"
16 )
17
18
19 func Example_rotation() {
20 n := 100
21 xmax := 2 * math.Pi
22
23
24 sin := func(n int, xmax float64) plotter.XYs {
25 xy := make(plotter.XYs, n)
26 for i := 0; i < n; i++ {
27 xy[i].X = xmax / float64(n) * float64(i)
28 xy[i].Y = math.Sin(xy[i].X) * 100
29 }
30 return xy
31 }
32
33
34 linePoints := sin(n, xmax)
35
36
37 labelPoints := sin(8, xmax)
38
39 p := plot.New()
40 p.Title.Text = "Rotation Example"
41 p.X.Label.Text = "X"
42 p.Y.Label.Text = "100 × Sine X"
43
44 l, err := plotter.NewLine(linePoints)
45 if err != nil {
46 log.Panic(err)
47 }
48 l.LineStyle.Width = vg.Points(1)
49 l.LineStyle.Color = color.RGBA{B: 255, A: 255}
50
51 labelData := plotter.XYLabels{
52 XYs: labelPoints,
53 Labels: []string{"0", "pi/4", "pi/2", "3pi/4", "pi", "5pi/4", "3pi/2", "7pi/4", "2pi"},
54 }
55
56 labels, err := plotter.NewLabels(labelData)
57 if err != nil {
58 log.Panic(err)
59 }
60
61 for i := range labels.TextStyle {
62 x := labels.XYs[i].X
63
64
65
66 labels.TextStyle[i].Rotation = math.Atan(math.Cos(x))
67 labels.TextStyle[i].XAlign = draw.XCenter
68 labels.TextStyle[i].YAlign = draw.YCenter
69
70 if x >= math.Pi {
71 labels.TextStyle[i].YAlign = draw.YTop
72 } else {
73 labels.TextStyle[i].YAlign = draw.YBottom
74 }
75 }
76
77 p.Add(l, labels)
78
79
80 p.Add(plotter.NewGlyphBoxes())
81
82 p.NominalX("0", "The number 1", "Number 2", "The number 3", "Number 4",
83 "The number 5", "Number 6")
84
85
86 p.X.Tick.Label.Rotation = math.Pi / 5
87 p.X.Tick.Label.YAlign = draw.YCenter
88 p.X.Tick.Label.XAlign = draw.XRight
89
90
91 p.Y.Tick.Label.Rotation = math.Pi / 2
92 p.Y.Tick.Label.XAlign = draw.XCenter
93 p.Y.Tick.Label.YAlign = draw.YBottom
94
95 err = p.Save(200, 150, "testdata/rotation.png")
96 if err != nil {
97 log.Panic(err)
98 }
99 }
100
View as plain text