douruduan8812 2017-11-29 16:28
浏览 169
已采纳

在gonum / plot中共享横坐标轴的多条线图

Is it possible to make multiple line plots with common abscissas axis in gonum/plot?

In matplotlib it would look like this. Sharing X axis

  • 写回答

1条回答 默认 最新

  • douyingyu5573 2017-12-28 12:54
    关注

    Yes, it is possible. You can use plot.Align:

    package main
    
    import (
        "math/rand"
        "os"
    
        "gonum.org/v1/plot"
        "gonum.org/v1/plot/plotter"
        "gonum.org/v1/plot/vg"
        "gonum.org/v1/plot/vg/draw"
        "gonum.org/v1/plot/vg/vgimg"
    )
    
    func main() {
        rand.Seed(int64(0))
    
        const rows, cols = 2, 1
        plots := make([][]*plot.Plot, rows)
        for j := 0; j < rows; j++ {
            plots[j] = make([]*plot.Plot, cols)
            for i := 0; i < cols; i++ {
    
                p := randomLinePlot(rand.Intn(10))
    
                // make sure the horizontal scales match
                p.X.Min = 0
                p.X.Max = 5
    
                plots[j][i] = p
            }
        }
    
        img := vgimg.New(vg.Points(150), vg.Points(175))
        dc := draw.New(img)
    
        t := draw.Tiles{
            Rows: rows,
            Cols: cols,
        }
    
        canvases := plot.Align(plots, t, dc)
        for j := 0; j < rows; j++ {
            for i := 0; i < cols; i++ {
                if plots[j][i] != nil {
                    plots[j][i].Draw(canvases[j][i])
                }
            }
        }
    
        w, err := os.Create("aligned.png")
        if err != nil {
            panic(err)
        }
    
        png := vgimg.PngCanvas{Canvas: img}
        if _, err := png.WriteTo(w); err != nil {
            panic(err)
        }
    }
    

    This generates the following plots in a single PNG file:

    Two aligned plots in a single file

    You can find another example below the GoDoc entry of plot.Align

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器