Skip to content

plotter: Is current "uniformness" of heatmap coloring intentional? #777

Open
@seiyab

Description

@seiyab

What are you trying to do?

Drawing heatmap.

What did you do?

func TestHeatMap(t *testing.T) {
	m := offsetUnitGrid{
		XOffset: -2,
		YOffset: -1,
		Data: mat.NewDense(3, 4, []float64{
			1, 2, 3, 4,
			5, 6, 7, 8,
			9, 10, 11, 12,
		}),
	}
	pal := myPalette{}
	h := plotter.NewHeatMap(m, pal)

	p := plot.New()
	p.Add(h)

	p.X.Padding = 0
	p.Y.Padding = 0

	img := vgimg.New(250, 175)
	dc := draw.New(img)
	p.Draw(dc)
	w, err := os.Create("golden_files/heatMap.png")
	if err != nil {
		log.Panic(err)
	}
	png := vgimg.PngCanvas{Canvas: img}
	if _, err = png.WriteTo(w); err != nil {
		t.Fatal(err)
	}
}

type myPalette struct{}

func (myPalette) Colors() []color.Color {
	return []color.Color{
		color.RGBA{R: 255, A: 255},
		color.RGBA{G: 255, A: 255},
		color.RGBA{B: 255, A: 255},
		color.RGBA{R: 255, G: 255, A: 255},
	}
}

// offsetUnitGrid is copied from https://github.com/gonum/plot/blob/b4fdc267610216647ec69681d47e431cf5bbed23/plotter/heat_test.go#L21

What did you expect to happen?

Each color is used "uniformly". I consider 3 cells for each colors (3 * 4 = 12) is "unform".
Following code comments says palette is scaled uniformly across the data range.

// ps scales the palette uniformly across the data range.

What actually happened?

2 cells for red, 4 cells for green, 4 cells for blue, 2 cells for yellow. This doesn't look uniform to me.
heatMap

Probably first color and last color have half chance to be used compared the other colors.
In my thought, following should be ps := float64(len(pal)) / (h.Max - h.Min)

plot/plotter/heat.go

Lines 157 to 158 in b4fdc26

// ps scales the palette uniformly across the data range.
ps := float64(len(pal)-1) / (h.Max - h.Min)

and following shouldbe col = pal[int((v-h.Min)*ps)] to get uniform.

plot/plotter/heat.go

Lines 222 to 224 in b4fdc26

default:
col = pal[int((v-h.Min)*ps+0.5)] // Apply palette scaling.
}

What version of Go and Gonum/plot are you using?

go version go1.22.0 darwin/arm64
gonum.org/v1/plot v0.14.0

Does this issue reproduce with the current master?

Not experimented yet. Maybe yes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions