2017-07-02 2 views
0

ループを使用して複数のプロットを追加しようとしていますが、行を挿入する方法がわかりません。私が作業しているコードは次のとおりです。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), 
} 

よう

何かが、私はそれが正しくないですけど、私はそれについて移動するかどうかはわかりませんではありません。

+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