Help us understand the problem. What is going on with this article?

Golangでグラフを描く

More than 1 year has passed since last update.

はじめに

golangでグラフを描く時,つまづいたので備忘録として残しておきます.
どうやらgonum plotが便利らしい.
しかし,古い記事だとgo getの場所がgoogle codeを参照しているようだ.
現在は,githubに移行しているので以下を実行.

go get gonum.org/v1/plot/...

二つのデータ群をplot

(8.0, 2.0)(3.0, 6.0)を軸にそれぞれclass1,class2として100個,ガウス分布に従って,学習データを生成したものをplotする.
以下がそのコードである.図の生成からplotまで,コメント文を参照してください.

package main

import (
    "image/color"![plot.png](https://qiita-image-store.s3.amazonaws.com/0/123283/3a191005-7612-21c4-88ef-aa6b3f3c5e29.png)

    "math/rand"
    "time"

    "gonum.org/v1/plot"
    "gonum.org/v1/plot/plotter"
    "gonum.org/v1/plot/vg"
)

func main() {

    // 図の生成
    p, err := plot.New()
    if err != nil {
        panic(err)
    }
    //label
    p.Title.Text = "Points Example"
    p.X.Label.Text = "X"
    p.Y.Label.Text = "Y"

    // 補助線
    p.Add(plotter.NewGrid())

    //クラス1
    x1, y1 := 8.0, 2.0

    //クラス2
    x2, y2 := 3.0, 6.0

    //各クラスのサンプル
    n := 200

    // 散布図の作成
    plot1, err := plotter.NewScatter(randomPoints(n, x1, y1))
    if err != nil {
        panic(err)
    }

    plot2, err := plotter.NewScatter(randomPoints(n, x2, y2))
    if err != nil {
        panic(err)
    }

    //色を指定する.
    plot1.GlyphStyle.Color = color.RGBA{R: 255, B: 128, A: 55}
    plot2.GlyphStyle.Color = color.RGBA{R: 155, B: 128, A: 255}

    //plot1,plot2をplot
    p.Add(plot1)
    p.Add(plot2)

    //label
    p.Legend.Add("class1", plot1)
    p.Legend.Add("class2", plot2)

    // 座標範囲
    p.X.Min = 0
    p.X.Max = 10
    p.Y.Min = 0
    p.Y.Max = 10

    // plot.pngに保存
    if err := p.Save(6*vg.Inch, 6*vg.Inch, "plot.png"); err != nil {
        panic(err)
    }

}

//ガウス分布
func random(axis float64) float64 {
    //分散
    dispersion := 1.0
    rand.Seed(time.Now().UnixNano())
    return rand.NormFloat64()*dispersion + axis
}

//学習データの生成
func randomPoints(n int, x, y float64) plotter.XYs {
    pts := make(plotter.XYs, n)
    for i := range pts {
        pts[i].X = random(x)
        pts[i].Y = random(y)
    }
    return pts
}

結果

plot.png

実際にplotすることができた.

参考文献

Go でグラフを plot するパッケージを試した
gonum

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away