/
s2d.go
317 lines (271 loc) · 6.78 KB
/
s2d.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright ©2016 The go-hep 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 hplot
import (
"image/color"
"math"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"gonum.org/v1/plot/vg/draw"
)
// S2D plots a set of 2-dim points with error bars.
type S2D struct {
Data plotter.XYer
// GlyphStyle is the style of the glyphs drawn
// at each point.
draw.GlyphStyle
// LineStyle is the style of the line drawn
// connecting each point.
// Use zero width to disable.
LineStyle draw.LineStyle
// XErrs is the x error bars plotter.
XErrs *plotter.XErrorBars
// YErrs is the y error bars plotter.
YErrs *plotter.YErrorBars
// Band displays a colored band between the y-min and y-max error bars.
Band *Band
// Steps controls the style of the connecting
// line (NoSteps, HiSteps, etc...)
Steps StepsKind
}
// withXErrBars enables the X error bars
func (pts *S2D) withXErrBars() error {
xerr, ok := pts.Data.(plotter.XErrorer)
if !ok {
return nil
}
type xerrT struct {
plotter.XYer
plotter.XErrorer
}
xplt, err := plotter.NewXErrorBars(xerrT{pts.Data, xerr})
if err != nil {
return err
}
pts.XErrs = xplt
return nil
}
// withYErrBars enables the Y error bars
func (pts *S2D) withYErrBars() error {
yerr, ok := pts.Data.(plotter.YErrorer)
if !ok {
return nil
}
type yerrT struct {
plotter.XYer
plotter.YErrorer
}
yplt, err := plotter.NewYErrorBars(yerrT{pts.Data, yerr})
if err != nil {
return err
}
pts.YErrs = yplt
return nil
}
// withBand enables the band between ymin-ymax error bars.
func (pts *S2D) withBand() error {
yerr, ok := pts.Data.(plotter.YErrorer)
if !ok {
return nil
}
var (
top plotter.XYs
bot plotter.XYs
)
switch pts.Steps {
case NoSteps:
top = make(plotter.XYs, pts.Data.Len())
bot = make(plotter.XYs, pts.Data.Len())
for i := range top {
x, y := pts.Data.XY(i)
ymin, ymax := yerr.YError(i)
top[i].X = x
top[i].Y = y + math.Abs(ymax)
bot[i].X = x
bot[i].Y = y - math.Abs(ymin)
}
case HiSteps:
top = make(plotter.XYs, 2*pts.Data.Len())
bot = make(plotter.XYs, 2*pts.Data.Len())
xerr := pts.Data.(plotter.XErrorer)
for i := range top {
idata := i / 2
x, y := pts.Data.XY(idata)
xmin, xmax := xerr.XError(idata)
ymin, ymax := yerr.YError(idata)
switch {
case i%2 != 0:
top[i].X = x + math.Abs(xmax)
top[i].Y = y + math.Abs(ymax)
bot[i].X = x + math.Abs(xmax)
bot[i].Y = y - math.Abs(ymin)
default:
top[i].X = x - math.Abs(xmin)
top[i].Y = y + math.Abs(ymax)
bot[i].X = x - math.Abs(xmin)
bot[i].Y = y - math.Abs(ymin)
}
}
}
pts.Band = NewBand(color.Gray{200}, top, bot)
return nil
}
// NewS2D creates a 2-dim scatter plot from a XYer.
func NewS2D(data plotter.XYer, opts ...Options) *S2D {
s := &S2D{
Data: data,
GlyphStyle: plotter.DefaultGlyphStyle,
}
s.GlyphStyle.Shape = draw.CrossGlyph{}
cfg := newConfig(opts)
s.Steps = cfg.steps
if cfg.bars.xerrs {
_ = s.withXErrBars()
}
if cfg.bars.yerrs {
_ = s.withYErrBars()
}
if cfg.band {
_ = s.withBand()
}
if cfg.glyph != (draw.GlyphStyle{}) {
s.GlyphStyle = cfg.glyph
}
return s
}
// Plot draws the Scatter, implementing the plot.Plotter
// interface.
func (pts *S2D) Plot(c draw.Canvas, plt *plot.Plot) {
trX, trY := plt.Transforms(&c)
if pts.Band != nil {
pts.Band.Plot(c, plt)
}
for i := 0; i < pts.Data.Len(); i++ {
x, y := pts.Data.XY(i)
c.DrawGlyph(pts.GlyphStyle, vg.Point{X: trX(x), Y: trY(y)})
}
if pts.LineStyle.Width > 0 {
data, err := plotter.CopyXYs(pts.Data)
if err != nil {
panic(err)
}
if pts.Steps == HiSteps {
xerr := pts.Data.(plotter.XErrorer)
dsteps := make(plotter.XYs, 0, 2*len(data))
for i, d := range data {
xmin, xmax := xerr.XError(i)
dsteps = append(dsteps, plotter.XY{X: d.X - xmin, Y: d.Y})
dsteps = append(dsteps, plotter.XY{X: d.X + xmax, Y: d.Y})
}
data = dsteps
}
line := plotter.Line{
XYs: data,
LineStyle: pts.LineStyle,
}
line.Plot(c, plt)
}
if pts.XErrs != nil {
pts.XErrs.LineStyle.Color = pts.GlyphStyle.Color
pts.XErrs.Plot(c, plt)
}
if pts.YErrs != nil {
pts.YErrs.LineStyle.Color = pts.GlyphStyle.Color
pts.YErrs.Plot(c, plt)
}
}
// DataRange returns the minimum and maximum
// x and y values, implementing the plot.DataRanger
// interface.
func (pts *S2D) DataRange() (xmin, xmax, ymin, ymax float64) {
if dr, ok := pts.Data.(plot.DataRanger); ok {
xmin, xmax, ymin, ymax = dr.DataRange()
} else {
xmin, xmax, ymin, ymax = plotter.XYRange(pts.Data)
}
if pts.XErrs != nil {
xmin1, xmax1, ymin1, ymax1 := pts.XErrs.DataRange()
xmin = math.Min(xmin1, xmin)
xmax = math.Max(xmax1, xmax)
ymin = math.Min(ymin1, ymin)
ymax = math.Max(ymax1, ymax)
}
if pts.YErrs != nil {
xmin1, xmax1, ymin1, ymax1 := pts.YErrs.DataRange()
xmin = math.Min(xmin1, xmin)
xmax = math.Max(xmax1, xmax)
ymin = math.Min(ymin1, ymin)
ymax = math.Max(ymax1, ymax)
}
return xmin, xmax, ymin, ymax
}
// GlyphBoxes returns a slice of plot.GlyphBoxes,
// implementing the plot.GlyphBoxer interface.
func (pts *S2D) GlyphBoxes(plt *plot.Plot) []plot.GlyphBox {
bs := make([]plot.GlyphBox, pts.Data.Len())
for i := 0; i < pts.Data.Len(); i++ {
x, y := pts.Data.XY(i)
bs[i].X = plt.X.Norm(x)
bs[i].Y = plt.Y.Norm(y)
bs[i].Rectangle = pts.GlyphStyle.Rectangle()
}
if pts.XErrs != nil {
bs = append(bs, pts.XErrs.GlyphBoxes(plt)...)
}
if pts.YErrs != nil {
bs = append(bs, pts.YErrs.GlyphBoxes(plt)...)
}
return bs
}
// Thumbnail the thumbnail for the Scatter,
// implementing the plot.Thumbnailer interface.
func (pts *S2D) Thumbnail(c *draw.Canvas) {
ymin := c.Min.Y
ymax := c.Max.Y
xmin := c.Min.X
xmax := c.Max.X
if pts.Band != nil {
box := []vg.Point{
{X: xmin, Y: ymin},
{X: xmax, Y: ymin},
{X: xmax, Y: ymax},
{X: xmin, Y: ymax},
{X: xmin, Y: ymin},
}
c.FillPolygon(pts.Band.FillColor, c.ClipPolygonXY(box))
}
if pts.LineStyle.Width != 0 {
ymid := c.Center().Y
line := []vg.Point{{X: xmin, Y: ymid}, {X: xmax, Y: ymid}}
c.StrokeLines(pts.LineStyle, c.ClipLinesX(line)...)
}
if pts.GlyphStyle != (draw.GlyphStyle{}) {
c.DrawGlyph(pts.GlyphStyle, c.Center())
if pts.YErrs != nil {
var (
yerrs = pts.YErrs
vsize = 0.5 * ((ymax - ymin) * 0.95)
x = c.Center().X
ylo = c.Center().Y - vsize
yup = c.Center().Y + vsize
xylo = vg.Point{X: x, Y: ylo}
xyup = vg.Point{X: x, Y: yup}
line = []vg.Point{xylo, xyup}
bar = c.ClipLinesY(line)
)
c.StrokeLines(yerrs.LineStyle, bar...)
for _, pt := range []vg.Point{xylo, xyup} {
if c.Contains(pt) {
c.StrokeLine2(yerrs.LineStyle,
pt.X-yerrs.CapWidth/2,
pt.Y,
pt.X+yerrs.CapWidth/2,
pt.Y,
)
}
}
}
}
}