Skip to content

Heatmap

When to use

Heatmaps are good for:

  • Correlation matrices between many variables.
  • Activity data over a two-dimensional grid (day × hour, row × column).
  • Confusion matrices and cross-tabulations.

Use ax.Heatmap(matrix, chart.HeatmapStyle{...}) where matrix[row][col] is the scalar value for that cell.


Basic example

Pairwise correlation matrix for four business metrics — positive correlations in warm colours, the negative churn correlation in cool.

package main

import (
	"github.com/goplotlib/goplotlib/chart"
	"github.com/goplotlib/goplotlib/colormap"
	"github.com/goplotlib/goplotlib/plot"
)

func main() {
	// Pearson correlations: Revenue, Profit, Active Users, Churn Rate
	matrix := [][]float64{
		{ 1.00,  0.82,  0.65, -0.43},
		{ 0.82,  1.00,  0.51, -0.37},
		{ 0.65,  0.51,  1.00, -0.71},
		{-0.43, -0.37, -0.71,  1.00},
	}
	labels := []string{"Revenue", "Profit", "Users", "Churn"}

	fig := plot.New(plot.WithWidth(600), plot.WithHeight(480))
	ax := fig.AddAxes()
	ax.Heatmap(matrix, chart.HeatmapStyle{
		RowLabels:      labels,
		ColLabels:      labels,
		ColorMap:       colormap.RdBu,
		DivergingScale: true,
		CellLabels:     true,
	})
	ax.SetTitle("Metric Correlation Matrix")
}

Heatmap


Colourmaps

Import "github.com/goplotlib/goplotlib/colormap" and pass one of the built-in maps:

ConstantDescription
colormap.ViridisPerceptually uniform, blue → green → yellow
colormap.PlasmaBlue → purple → orange
colormap.GreysWhite → black (sequential)
colormap.BluesWhite → dark blue (sequential)
colormap.RdBuRed → white → blue (diverging)
ax.Heatmap(matrix, chart.HeatmapStyle{
	ColorMap:       colormap.RdBu,
	DivergingScale: true,
})

Use DivergingScale: true with diverging maps to centre the colour scale at zero.


Cell labels

// Print the numeric value inside each cell
ax.Heatmap(matrix, chart.HeatmapStyle{CellLabels: true})

Text colour is automatically chosen as white or dark based on cell luminance.


Style reference

chart.HeatmapStyle fields:

FieldTypeDefaultDescription
RowLabels[]stringindexLabels for each row (y-axis)
ColLabels[]stringindexLabels for each column (x-axis)
ColorMapcolormap.ColorMapViridisColour map to use
CellLabelsboolfalsePrint the numeric value inside each cell
DivergingScaleboolfalseCentre normalisation at zero (for diverging maps)