File: example_align_test.go

package info (click to toggle)
golang-gonum-v1-plot 0.7.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 13,980 kB
  • sloc: sh: 81; makefile: 13
file content (92 lines) | stat: -rw-r--r-- 1,969 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright ©2017 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package plot_test

import (
	"math"
	"os"

	"gonum.org/v1/plot"
	"gonum.org/v1/plot/vg"
	"gonum.org/v1/plot/vg/draw"
	"gonum.org/v1/plot/vg/vgimg"
)

func ExampleAlign() {
	const rows, cols = 4, 3
	plots := make([][]*plot.Plot, rows)
	for j := 0; j < rows; j++ {
		plots[j] = make([]*plot.Plot, cols)
		for i := 0; i < cols; i++ {
			if i == 0 && j == 2 {
				// This shows what happens when there are nil plots.
				continue
			}

			p, err := plot.New()
			if err != nil {
				panic(err)
			}

			if j == 0 && i == 2 {
				// This shows what happens when the axis padding
				// is different among plots.
				p.X.Padding, p.Y.Padding = 0, 0
			}

			if j == 1 && i == 1 {
				// To test the Align function, we make the axis labels
				// on one of the plots stick out.
				p.Y.Max = 1e9
				p.X.Max = 1e9
				p.X.Tick.Label.Rotation = math.Pi / 2
				p.X.Tick.Label.XAlign = draw.XRight
				p.X.Tick.Label.YAlign = draw.YCenter
				p.X.Tick.Label.Font.Size = 8
				p.Y.Tick.Label.Font.Size = 8
			} else {
				p.Y.Max = 1e9
				p.X.Max = 1e9
				p.X.Tick.Label.Font.Size = 1
				p.Y.Tick.Label.Font.Size = 1
			}

			plots[j][i] = p
		}
	}

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

	t := draw.Tiles{
		Rows:      rows,
		Cols:      cols,
		PadX:      vg.Millimeter,
		PadY:      vg.Millimeter,
		PadTop:    vg.Points(2),
		PadBottom: vg.Points(2),
		PadLeft:   vg.Points(2),
		PadRight:  vg.Points(2),
	}

	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("testdata/align.png")
	if err != nil {
		panic(err)
	}
	defer w.Close()
	png := vgimg.PngCanvas{Canvas: img}
	if _, err := png.WriteTo(w); err != nil {
		panic(err)
	}
}