0

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

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

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

4

1 回答 1

0

我认为您想先将数据提取到 a[]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...)
于 2017-07-02T16:36:39.670 回答