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( |
Width of the marks. One of:
|
height |
anyOf( |
Height of the marks. One of:
|
align |
The horizontal alignment of the text or ranged marks (area, bar, image, rect, rule). One of Note: Expression reference is not supported for range marks. |
|
baseline |
anyOf( |
For text marks, the vertical text baseline. One of For range marks, the vertical alignment of the marks. One of Note: Expression reference is not supported for range marks. |
cornerRadius |
anyOf( |
The radius in pixels of rounded rectangles or arcs’ corners. Default value: |
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