Multiple line plots sharing abscissas axis in gonum/plot

白昼怎懂夜的黑 提交于 2019-12-14 00:07:25

问题


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

In matplotlib it would look like this.


回答1:


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:

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



来源:https://stackoverflow.com/questions/47557438/multiple-line-plots-sharing-abscissas-axis-in-gonum-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!