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
solved Multiple line plots sharing abscissas axis in gonum/plot