...
1
2
3
4
5 package plotter_test
6
7 import (
8 "image/color"
9 "log"
10
11 "golang.org/x/exp/rand"
12
13 "gonum.org/v1/plot"
14 "gonum.org/v1/plot/plotter"
15 "gonum.org/v1/plot/vg"
16 "gonum.org/v1/plot/vg/draw"
17 )
18
19 func ExampleLine_stepLine() {
20 rnd := rand.New(rand.NewSource(1))
21
22
23
24 randomPoints := func(n int, x float64) plotter.XYs {
25 pts := make(plotter.XYs, n)
26 for i := range pts {
27 pts[i].X = float64(i) + x
28 pts[i].Y = 5. + 10*rnd.Float64()
29 }
30 return pts
31 }
32
33 n := 4
34
35 p := plot.New()
36 p.Title.Text = "Step Example"
37 p.X.Label.Text = "X"
38 p.Y.Label.Text = "Y"
39 p.Add(plotter.NewGrid())
40
41 stepPre, err := plotter.NewLine(randomPoints(n, 0))
42 if err != nil {
43 log.Panic(err)
44 }
45 stepPre.StepStyle = plotter.PreStep
46 stepPre.FillColor = color.RGBA{R: 196, G: 255, B: 196, A: 255}
47
48 stepMid, err := plotter.NewLine(randomPoints(n, 3.5))
49 if err != nil {
50 log.Panic(err)
51 }
52 stepMid.StepStyle = plotter.MidStep
53 stepMid.LineStyle = draw.LineStyle{Color: color.RGBA{R: 196, B: 128, A: 255}, Width: vg.Points(1)}
54
55 stepMidFilled, err := plotter.NewLine(randomPoints(n, 7))
56 if err != nil {
57 log.Panic(err)
58 }
59 stepMidFilled.StepStyle = plotter.MidStep
60 stepMidFilled.LineStyle = draw.LineStyle{Color: color.RGBA{R: 196, B: 128, A: 255}, Width: vg.Points(1)}
61 stepMidFilled.FillColor = color.RGBA{R: 255, G: 196, B: 196, A: 255}
62
63 stepPost, err := plotter.NewLine(randomPoints(n, 10.5))
64 if err != nil {
65 log.Panic(err)
66 }
67 stepPost.StepStyle = plotter.PostStep
68 stepPost.LineStyle.Width = 0
69 stepPost.FillColor = color.RGBA{R: 196, G: 196, B: 255, A: 255}
70
71 p.Add(stepPre, stepMid, stepMidFilled, stepPost)
72 p.Legend.Add("pre", stepPre)
73 p.Legend.Add("mid", stepMid)
74 p.Legend.Add("midFilled", stepMidFilled)
75 p.Legend.Add("post", stepPost)
76 p.Legend.Top = true
77 p.Y.Max = 20
78 p.Y.Min = 0
79
80 err = p.Save(200, 200, "testdata/step.png")
81 if err != nil {
82 log.Panic(err)
83 }
84 }
85
View as plain text