Skip to content

Instantly share code, notes, and snippets.

@cipepser
Created March 23, 2017 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cipepser/9515002d792508b91e54766930751e37 to your computer and use it in GitHub Desktop.
Save cipepser/9515002d792508b91e54766930751e37 to your computer and use it in GitHub Desktop.
package main
import (
"github.com/gonum/plot"
"github.com/gonum/plot/plotter"
"github.com/gonum/plot/plotutil"
"github.com/gonum/plot/vg"
)
func main() {
group := plotter.Values{20, 35, 30, 35, 27} // 描画対象
p, err := plot.New()
if err != nil {
panic(err)
}
p.Title.Text = "Bar chart" // グラフのタイトル
p.X.Label.Text = "No." // X軸名
p.Y.Label.Text = "Heights" // Y軸名
w := vg.Points(10) // 棒グラフの幅
// groupの各値について棒グラフを生成
bars, err := plotter.NewBarChart(group, w)
if err != nil {
panic(err)
}
bars.LineStyle.Width = vg.Length(0) // 棒グラフの枠線の太さ
bars.Color = plotutil.Color(3) // 棒グラフの色。0から6まででplotutilにSoftColorsとして定義されている。
bars.Offset = 0 // 棒グラフを表示する位置のオフセット(X方向)。複数のグループを並べたいときは-wなどで位置を調整する。
bars.Horizontal = false // trueにすると横向きの棒グラフになる。
p.Add(bars) // 棒グラフをプロットする
p.Legend.Add("values", bars) // 判例名。日本語だと表示されないみたい。
p.Legend.Top = true // 判例の位置調整。falseだと下側。
p.Legend.Left = false // 判例の位置調整。falseだと右側。
p.NominalX("One", "Two", "Three", "Four", "Five") // 各値のラベル(X軸)
// 保存処理。拡張子は以下をサポートしているとのこと。
// .eps, .jpg, .jpeg, .pdf, .png, .svg, .tif and .tiff
if err := p.Save(5*vg.Inch, 3*vg.Inch, "barchart.png"); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment