Removing space between marks in altair visualization

Viewed 158

I have a visualization where I am trying to increase mark size and also have them touching (decrease the space in-between the marks) using altair. Is there a way to do this without simply using 'size='? I would like to make this adaptable to a function and not hardcode the size, but have it dependable on the data given.

Here is my current code so far:

alt.Chart(df).mark_square().encode(
x=alt.X('p1:N'),
y=alt.Y('p2:N'),
color=alt.Color('weight:O', legend=alt.Legend(type='symbol'), scale=alt.Scale(scheme='blues'))
               
).properties(
    height=500,
    width=500
 )

My current output:

enter image description here

My intended output I am trying to achieve:

enter image description here

2 Answers

If you use mark_rect() rather than mark_square() with a nominal or ordinal encoding, the size of each mark will automatically be adjusted to fill the space.

size is propierty from Altair then is used to manipulate Marks size and so it is for point/circle/square, please check altair-viz.github.io/user_guide/marks.html?highlight=mark_square. To show how it could be used through a function I did this:

def plot_mark_squares(df, bandsize=0, size=30, width=500, height=500):  
    alt.Chart(df).mark_square().encode(
    x=alt.X('p1:N'),
    y=alt.Y('p2:N'),
    color=alt.Color('weight:O', legend=alt.Legend(type='symbol'), scale=alt.Scale(scheme='blues'))
    ).properties(
           height=500,
           width=500,
           bandsize=bandsize,
           size=size
    )

plot_mark_squares(df, 0.1, 50)
Related