go-hep.org/x/hep@v0.38.1/hplot/hplot.go (about) 1 // Copyright ©2016 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Copyright ©2012 The Plotinum Authors. All rights reserved. 6 // Use of this source code is governed by an MIT-style license 7 // that can be found in the LICENSE file. 8 9 //go:generate go tool github.com/campoy/embedmd -w README.md 10 11 package hplot // import "go-hep.org/x/hep/hplot" 12 13 import ( 14 "bytes" 15 16 "gonum.org/v1/plot/plotter" 17 "gonum.org/v1/plot/vg" 18 "gonum.org/v1/plot/vg/draw" 19 ) 20 21 // Show displays the plot according to format, returning the raw bytes and 22 // an error, if any. 23 // 24 // If format is the empty string, then "png" is selected. 25 // The list of accepted format strings is the same one than from 26 // the gonum.org/v1/plot/vg/draw.NewFormattedCanvas function. 27 func Show(p Drawer, w, h vg.Length, format string) ([]byte, error) { 28 w, h = Dims(w, h) 29 30 if format == "" { 31 format = "png" 32 } 33 34 c, err := draw.NewFormattedCanvas(w, h, format) 35 if err != nil { 36 return nil, err 37 } 38 39 p.Draw(draw.New(c)) 40 out := new(bytes.Buffer) 41 _, err = c.WriteTo(out) 42 if err != nil { 43 return nil, err 44 } 45 return out.Bytes(), nil 46 } 47 48 // zip zips together 2 slices and implements the plotter.XYer interface. 49 type zip struct { 50 x []float64 51 y []float64 52 } 53 54 // Len implements the plotter.XYer interface 55 func (z zip) Len() int { return len(z.x) } 56 57 // XY implements the plotter.XYer interface 58 func (z zip) XY(i int) (x, y float64) { return z.x[i], z.y[i] } 59 60 // ZipXY zips together 2 slices x and y in such a way to implement the 61 // plotter.XYer interface. 62 // 63 // ZipXY panics if the slices are not of the same length. 64 func ZipXY(x, y []float64) plotter.XYer { 65 if len(x) != len(y) { 66 panic("hplot: slices length differ") 67 } 68 return zip{x: x, y: y} 69 }