dph6308 2017-07-02 15:56
浏览 40
已采纳

Gonum绘图通过切片循环

I'm trying to add multiple plots by using a loop, but I can't seem to figure out how to put the lines in. Here is the code I'm working on:

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

My getPoints function looks like this:

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
}

I get an error when trying to put a loop where the commented section is. I know this should be fairly straightforward. Perhaps a loop prior to this to get the list of lines?

Something like

for i, process := range processes {
    return "title", getPoints(process),
}

Obviously I know that isn't correct, but not I'm not sure how to go about it.

  • 写回答

1条回答 默认 最新

  • douzi9744 2017-07-02 16:36
    关注

    I think you want to first extract your data into a []interface{}, and then call into AddLinePoints. Roughly (I didn't test):

    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...)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵