diff --git a/plotter/heat_test.go b/plotter/heat_test.go index e77e8b34..b2a25efd 100644 --- a/plotter/heat_test.go +++ b/plotter/heat_test.go @@ -7,14 +7,17 @@ package plotter import ( "fmt" "log" + "os" "testing" "github.com/gonum/matrix/mat64" "github.com/gonum/plot" "github.com/gonum/plot/internal/cmpimg" "github.com/gonum/plot/palette" + "github.com/gonum/plot/vg" "github.com/gonum/plot/vg/draw" "github.com/gonum/plot/vg/recorder" + "github.com/gonum/plot/vg/vgimg" ) type offsetUnitGrid struct { @@ -61,24 +64,44 @@ func ExampleHeatMap() { p.Add(h) // Create a legend. - rng := h.Max - h.Min thumbs := PaletteThumbnailers(pal) for i := len(thumbs) - 1; i >= 0; i-- { t := thumbs[i] - fmin := float64(i) / float64(len(thumbs)) - fmax := float64(i+1) / float64(len(thumbs)) - p.Legend.Add(fmt.Sprintf("%.2g - %.2g", h.Min+fmin*rng, h.Min+fmax*rng), t) + if i != 0 && i != len(thumbs)-1 { + p.Legend.Add("", t) + continue + } + var val float64 + switch i { + case 0: + val = h.Min + case len(thumbs) - 1: + val = h.Max + } + p.Legend.Add(fmt.Sprintf("%.2g", val), t) } + // This is the width of the legend, experimentally determined. + const legendWidth = 1.25 * vg.Centimeter + // Slide the legend over so it doesn't overlap the HeatMap. + p.Legend.XOffs = legendWidth p.X.Padding = 0 p.Y.Padding = 0 p.X.Max = 1.5 p.Y.Max = 1.5 - err = p.Save(250, 200, "testdata/heatMap.png") + img := vgimg.New(250, 175) + dc := draw.New(img) + dc = draw.Crop(dc, 0, -legendWidth, 0, 0) // Make space for the legend. + p.Draw(dc) + w, err := os.Create("testdata/heatMap.png") if err != nil { log.Panic(err) } + png := vgimg.PngCanvas{Canvas: img} + if _, err = png.WriteTo(w); err != nil { + log.Panic(err) + } } func TestHeatMap(t *testing.T) { diff --git a/plotter/testdata/heatMap_golden.png b/plotter/testdata/heatMap_golden.png index 2af6373f..943fae87 100644 Binary files a/plotter/testdata/heatMap_golden.png and b/plotter/testdata/heatMap_golden.png differ