-
Notifications
You must be signed in to change notification settings - Fork 1
/
representing_time_series_data.go
63 lines (53 loc) · 1.36 KB
/
representing_time_series_data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"github.com/kniren/gota/dataframe"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"image/color"
"log"
"os"
)
func main() {
// Open the CSV file.
passengersFile, err := os.Open("AirPassengers.csv")
if err != nil {
log.Fatal(err)
}
defer passengersFile.Close()
// Create a dataframe from the CSV file.
passengersDF := dataframe.ReadCSV(passengersFile)
// As a sanity check, display the records to stdout.
// Gota will format the dataframe for pretty printing.
fmt.Println(passengersDF)
// Extract the number of passengers column.
yVals := passengersDF.Col("AirPassengers").Float()
// pts will hold the values for plotting.
pts := make(plotter.XYs, passengersDF.Nrow())
// Fill pts with data.
for i, floatVal := range passengersDF.Col("time").Float() {
pts[i].X = floatVal
pts[i].Y = yVals[i]
}
// Create the plot.
p, err := plot.New()
if err != nil {
log.Fatal(err)
}
p.X.Label.Text = "time"
p.Y.Label.Text = "passengers"
p.Add(plotter.NewGrid())
// Add the line plot points for the time series.
l, err := plotter.NewLine(pts)
if err != nil {
log.Fatal(err)
}
l.LineStyle.Width = vg.Points(1)
l.LineStyle.Color = color.RGBA{B: 255, A: 255}
// Save the plot to a PNG file.
p.Add(l)
if err := p.Save(10*vg.Inch, 4*vg.Inch, "passengers_ts.png"); err != nil {
log.Fatal(err)
}
}