Skip to content

Commit

Permalink
Merge c1ce72e into af3269f
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Jul 20, 2018
2 parents af3269f + c1ce72e commit fb30f18
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
7 changes: 5 additions & 2 deletions plotter/histogram.go
Expand Up @@ -150,7 +150,7 @@ func (h *Histogram) Thumbnail(c *draw.Canvas) {
// then a reasonable default is used. The
// default is the square root of the sum of
// the y values.
func binPoints(xys XYer, n int) ([]HistogramBin, float64) {
func binPoints(xys XYer, n int) (bins []HistogramBin, width float64) {
xmin, xmax := Range(XValues{xys})
if n <= 0 {
m := 0.0
Expand All @@ -164,9 +164,12 @@ func binPoints(xys XYer, n int) ([]HistogramBin, float64) {
n = 1
}

bins := make([]HistogramBin, n)
bins = make([]HistogramBin, n)

w := (xmax - xmin) / float64(n)
if w == 0 {
w = 1
}
for i := range bins {
bins[i].Min = xmin + float64(i)*w
bins[i].Max = xmin + float64(i+1)*w
Expand Down
31 changes: 31 additions & 0 deletions plotter/histogram_test.go
Expand Up @@ -10,6 +10,7 @@ import (
"math"
"math/rand"
"testing"
"time"

"gonum.org/v1/plot"
"gonum.org/v1/plot/internal/cmpimg"
Expand Down Expand Up @@ -62,3 +63,33 @@ func ExampleHistogram() {
func TestHistogram(t *testing.T) {
cmpimg.CheckPlot(ExampleHistogram, t, "histogram.png")
}

func TestSingletonHistogram(t *testing.T) {
done := make(chan struct{}, 1)
go func() {
defer close(done)
p, err := plot.New()
if err != nil {
t.Fatalf("unexpected error from plot.New: %v", err)
}

hist, err := NewHist(Values([]float64{1.0}), 60)
if err != nil {
t.Fatalf("unexpected error from NewHist: %v", err)
}
hist.Normalize(1)

p.Add(hist)

_, err = p.WriterTo(4*vg.Inch, 4*vg.Inch, "png")
if err != nil {
t.Fatalf("unexpected error from WriterTo: %v", err)
}
}()

select {
case <-time.After(10 * time.Second):
t.Error("histogram timed out")
case <-done:
}
}

0 comments on commit fb30f18

Please sign in to comment.