在gonum / plot中共享横坐标轴的多条线图 [英] Multiple line plots sharing abscissas axis in gonum/plot

查看:301
本文介绍了在gonum / plot中共享横坐标轴的多条线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在gonum / plot中创建多个具有共同横坐标轴的线图?



在matplotlib中,它看起来像这样。

解决方案

是的,这是可能的。您可以使用 plot.Align

 包主

进口(
math / rand
os

gonum.org/v1/plot
gonum.org/v1/绘图/绘图仪
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
plot:= make([] [] * plot.Plot,rows)
for j:= 0; j<行; j ++ {
plot [j] = make([] * plot.Plot,cols)
for i:= 0;我<的cols; i ++ {

p:= randomLinePlot(rand.Intn(10))

//确保水平比例匹配
pXMin = 0
pX Max = 5

地块[j] [i] = p
}
}

img:= vgimg.New(vg.Points(150 ),vg.Points(175))
dc:= draw.New(img)

t:= draw.Tiles {
行:行,
Cols: cols,
}

canvases:= plot.Align(plots,t,dc)
for j:= 0; j<行; j ++ {
for i:= 0;我<的cols; i ++ {
if plot [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)
}
}

这会在单个PNG文件中生成以下图:



您可以在 plot.Align


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

In matplotlib it would look like this.

解决方案

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

这篇关于在gonum / plot中共享横坐标轴的多条线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆