Gonum Plot Loop Through Slice

时间:2017-07-02 15:56:38

标签: go gonum

我正在尝试使用循环添加多个绘图,但我似乎无法弄清楚如何放入行。这是我正在处理的代码:

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),
}

显然我知道这是不正确的,但不是我不确定该怎么做。

1 个答案:

答案 0 :(得分:0)

我认为您希望首先将数据提取到[]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...)