Infinite loop drawing histogram
Created by: rosshemsley
The following code seems to loop infinitely on p.Save
:
p, err := plot.New()
if err != nil {
panic(err)
}
hist, err := plotter.NewHist(plotter.Values([]float64{1.0}), 60)
hist.Normalize(1)
p.Add(hist)
err = p.Save(4*vg.Inch, 4*vg.Inch, "hist.png")
if err != nil {
panic(err)
}
The problem is in the function below:
// Normalize normalizes the histogram so that the
// total area beneath it sums to a given value.
func (h *Histogram) Normalize(sum float64) {
mass := 0.0
for _, b := range h.Bins {
mass += b.Weight
}
for i := range h.Bins {
h.Bins[i].Weight *= sum / (h.Width * mass)
}
}
Since h.Width
is zero, an infinity appears that breaks the render to the canvas
It's not totally obvious what should be done here, perhaps set the bin width to 1.0 for trivial histograms?