Rect#

The rect mark represents an arbitrary rectangle.

Rect Mark Properties#

Click to show code
import altair as alt

x_slider = alt.binding_range(min=1, max=100, step=1, name="x")
x_var = alt.param(bind=x_slider, value=25)

x2_slider = alt.binding_range(min=1, max=100, step=1, name="x2")
x2_var = alt.param(bind=x2_slider, value=75)

y_slider = alt.binding_range(min=1, max=100, step=1, name="y")
y_var = alt.param(bind=y_slider, value=25)

y2_slider = alt.binding_range(min=1, max=100, step=1, name="y2")
y2_var = alt.param(bind=y2_slider, value=75)

cornerRadius_slider = alt.binding_range(min=0, max=50, step=1)
cornerRadius_var = alt.param(bind=cornerRadius_slider, value=0, name="cornerRadius")

alt.Chart().mark_rect(cornerRadius=cornerRadius_var, color="orange").encode(
    x=alt.XDatum(x_var, type="quantitative", scale=alt.Scale(domain=[0, 100])),
    x2=alt.X2Datum(x2_var),
    y=alt.XDatum(y_var, type="quantitative", scale=alt.Scale(domain=[0, 100])),
    y2=alt.X2Datum(y2_var),
).add_params(x_var, x2_var, y_var, y2_var, cornerRadius_var)

A rect mark definition can contain any standard mark properties and the following special properties:

Click to show table

Property

Type

Description

width

anyOf(number, ExprRef, RelativeBandSize)

Width of the marks. One of:

  • A number representing a fixed pixel width.

  • A relative band size definition. For example, {band: 0.5} represents half of the band.

height

anyOf(number, ExprRef, RelativeBandSize)

Height of the marks. One of:

  • A number representing a fixed pixel height.

  • A relative band size definition. For example, {band: 0.5} represents half of the band

align

anyOf(Align, ExprRef)

The horizontal alignment of the text or ranged marks (area, bar, image, rect, rule). One of "left", "right", "center".

Note: Expression reference is not supported for range marks.

baseline

anyOf(TextBaseline, ExprRef)

For text marks, the vertical text baseline. One of "alphabetic" (default), "top", "middle", "bottom", "line-top", "line-bottom", or an expression reference that provides one of the valid values. The "line-top" and "line-bottom" values operate similarly to "top" and "bottom", but are calculated relative to the lineHeight rather than fontSize alone.

For range marks, the vertical alignment of the marks. One of "top", "middle", "bottom".

Note: Expression reference is not supported for range marks.

cornerRadius

anyOf(number, ExprRef)

The radius in pixels of rounded rectangles or arcs’ corners.

Default value: 0

Examples#

Heatmap#

Using the rect marks with discrete fields on x and y channels creates a heatmap.

import altair as alt
from vega_datasets import data

source = data.seattle_weather()

alt.Chart(source).mark_rect().encode(
    alt.X("date(date):O").axis(labelAngle=0, format="%e").title("Day"),
    alt.Y("month(date):O").title("Month"),
    alt.Color("max(temp_max):Q").title("Max Temp"),
)

Ranged Rectangles#

Specifying both x and x2 and/or y and y2 creates a rectangle that spans over certain x and/or y values.

For example, we can use rect to create an annotation layer that provides a shading between global min and max values.

import altair as alt
from vega_datasets import data

source = data.cars()

point = alt.Chart(source).mark_point().encode(
    x="Horsepower:Q",
    y="Miles_per_Gallon:Q",
)

rect = alt.Chart(source).mark_rect().encode(
    y="max(Miles_per_Gallon)",
    y2="min(Miles_per_Gallon)",
    opacity=alt.value(0.2),
)

point + rect