Altair: Adjust marker width in heat map (mark_rect) ?

Created on 8 Mar 2019  路  4Comments  路  Source: altair-viz/altair

Hi,

is it possible to adjust the width of the rectangles in a mark_rect() heat map, so that they are not square anymore?

This would be useful for cases where there are many more values on the x axis than on the y axis (as in a phenotypic profile (my use case)).
Many thanks in advance for your help.

Kind regards,
Axel

Most helpful comment

Cool, that totally works!

Thanks a lot for the quick and illustrative answer!

Awesome project.

Kind regards,
Axel

All 4 comments

The easiest way to do this is to explicitly set the width and height of the chart; e.g.

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(-20, 20), 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().encode(
    x='x:O',
    y='y:O',
    color='z:Q'
).properties(
    width=400,
    height=200
)

visualization 49

Cool, that totally works!

Thanks a lot for the quick and illustrative answer!

Awesome project.

Kind regards,
Axel

Well, my question is regarding setting the size (width and height) of each of the cells in mark_rect(). How can we do that such that the size is fixed, and the overall heatmap just gets bigger if we have more boxes to show?

(For future searches, this last question is answered here: https://github.com/altair-viz/altair/issues/1618#issuecomment-513424086)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

breadbaron picture breadbaron  路  4Comments

tonylee3399 picture tonylee3399  路  3Comments

morberg picture morberg  路  3Comments

floringogianu picture floringogianu  路  3Comments

zanarmstrong picture zanarmstrong  路  4Comments