Altair: Space between rectangles / bars?

Created on 22 Jan 2019  Â·  5Comments  Â·  Source: altair-viz/altair

Sorry if this is covered elsewhere, but I wasn't able to find it anywhere. When using mark_bar() or mark_rect() to create a heatmap, is it possible to remove the spacing between rectangles/bars?

In my case I'm plotting a 2-D density and the rectangle boundaries distract from the density.
thanks!

visualization 366

question

Most helpful comment

A better solution would be to adjust the inner padding for x and y band-ordinal scales using the bandPaddingInner, which defaults to 0.1 (docs).

alt.Chart(data).mark_rect().encode(
  x='x:O',
  y='y:O',
  fill='z:Q'
).configure_scale(bandPaddingInner=0)

All 5 comments

It looks like the size property is not applied to rect or bar marks. But one way you can do this is to encode both the fill and the stroke, and make the stroke fill the space between marks. For example:

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

x, y = np.meshgrid(np.arange(30), np.arange(30))
z = np.exp(-(x - 14)**2 / (5 ** 2) - (y - 22) ** 2 / (5 ** 2))
data = pd.DataFrame({
    'x': x.ravel(),
    'y': y.ravel(),
    'z': z.ravel()
})

alt.Chart(data).mark_rect(strokeWidth=3).encode(
  x='x:O',
  y='y:O',
  fill='z:Q',
  stroke='z:Q'
)

visualization 25

Thanks for looking into that @jakevdp . This solution works great

A better solution would be to adjust the inner padding for x and y band-ordinal scales using the bandPaddingInner, which defaults to 0.1 (docs).

alt.Chart(data).mark_rect().encode(
  x='x:O',
  y='y:O',
  fill='z:Q'
).configure_scale(bandPaddingInner=0)

Thanks @mattjin – I didn't know about that configuration.

Just a quick note for anyone else who comes across this--the bandPaddingInner solution didn't work for me, but the fill and stroke solution worked fine. (I guess there was some change in the code, or there's something different about my plot.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HalukaMB picture HalukaMB  Â·  3Comments

nielsmde picture nielsmde  Â·  4Comments

dzonimn picture dzonimn  Â·  3Comments

breadbaron picture breadbaron  Â·  4Comments

tonylee3399 picture tonylee3399  Â·  3Comments