Skip to content

Commit

Permalink
Slide legend over
Browse files Browse the repository at this point in the history
  • Loading branch information
ctessum committed May 1, 2017
1 parent e25ebac commit 28dbf83
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions plotter/heat_test.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
Binary file modified plotter/testdata/heatMap_golden.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 28dbf83

Please sign in to comment.