4

Is it possible to add Black Grid lines to an Altair Heatmap?

I know that it is possible to add spacing between cells with scale.bandPaddingInner and I was thinking that maybe it would be possible to color that spacing with black?

What I want is something like this:Heatmap with Black Grid lines

Alina V
  • 119
  • 5

1 Answers1

7

Yes, you can do this with the stroke property of the rect mark. Adapting the heatmap example from Altair's documentation:

import altair as alt
import numpy as np
import pandas as pd

# Compute x^2 + y^2 across a 2D grid
x, y = np.meshgrid(range(-5, 5), range(-5, 5))
z = x ** 2 + y ** 2

# Convert this grid to columnar data expected by Altair
source = pd.DataFrame({'x': x.ravel(),
                     'y': y.ravel(),
                     'z': z.ravel()})

alt.Chart(source).mark_rect(stroke='black', strokeWidth=2).encode(
    x='x:O',
    y='y:O',
    color='z:Q'
)

enter image description here

jakevdp
  • 77,104
  • 11
  • 125
  • 160