2017-07-02 70 views
0

我想通過使用循環添加多個圖,但我似乎無法弄清楚如何將線放入。以下是我正在處理的代碼:Gonum Plot循環通過切片

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

很明顯,我知道這是不正確的,但我不是不知道如何去做。

+0

plotutil.AddLinePoints的什麼接口?可變參數函數?然後通過'plotutil.AddLinePoints(p, 「Test」,getPoints(processes [1])...)' – mattn

+0

Variadic。我可以刪除標題,因爲我在技術上不需要它們。 – user8244734

回答

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...) 
+0

這正是我想要做的。 – user8244734