我正在尝试使用循环添加多个图,但我似乎无法弄清楚如何放入线条。这是我正在处理的代码:
func plot_stochastic_processes(processes [][]float64, title string) {
p, err := plot.New()
if err != nil {
panic(err)
}
p.Title.Text = title
p.X.Label.Text = "X"
p.Y.Label.Text = "Y"
err = plotutil.AddLinePoints(p,
"Test", getPoints(processes[1]),
//Need to figure out how to loop through processes
)
if err != nil {
panic(err)
}
// Save the plot to a PNG file.
if err := p.Save(4*vg.Inch, 4*vg.Inch, "points.png"); err != nil {
panic(err)
}
}
我的 getPoints 函数如下所示:
func getPoints(line []float64) plotter.XYs {
pts := make(plotter.XYs, len(line))
for j, k := range line {
pts[j].X = float64(j)
pts[j].Y = k
}
return pts
}
尝试在注释部分所在的位置放置循环时出现错误。我知道这应该相当简单。也许在此之前的循环来获取行列表?
类似的东西
for i, process := range processes {
return "title", getPoints(process),
}
显然我知道这是不正确的,但我不知道该怎么做。
我认为您想先将数据提取到[]interface{}
中,然后调用AddLinePoints。粗略(我没有测试(:
lines := make([]interface{},0)
for i, v := range processes {
lines = append(lines, "Title" + strconv.Itoa(i))
lines = append(lines, getPoints(v))
}
plotutil.AddLinePoints(p, lines...)