Gonum 通过切片绘制循环
Gonum Plot Loop Through Slice
我正在尝试使用循环添加多个绘图,但我似乎无法弄清楚如何放入线条。这是我正在处理的代码:
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...)
我正在尝试使用循环添加多个绘图,但我似乎无法弄清楚如何放入线条。这是我正在处理的代码:
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...)