Marks#
We saw in Encodings that the encode()
method is
used to map columns to visual attributes of the plot.
The mark
property is what specifies how exactly those attributes
should be represented on the plot.
Altair provides a number of basic mark properties (the mark properties column links to the Vega-Lite documentation that allows you to interactively explore the effects of modifying each property):
Mark |
Description |
Example |
Mark Properties |
---|---|---|---|
|
A pie chart. |
||
|
A filled area plot. |
||
|
A bar plot. |
||
|
A scatter plot with filled circles. |
||
|
A geographic shape |
||
|
A scatter plot with image markers. |
||
|
A line plot. |
||
|
A scatter plot with configurable point shapes. |
||
|
A filled rectangle, used for heatmaps |
||
|
A vertical or horizontal line spanning the axis. |
||
|
A scatter plot with filled squares. |
N/A |
|
|
A scatter plot with points represented by text. |
||
|
A vertical or horizontal tick mark. |
||
|
A line with variable widths. |
In addition, Altair provides the following compound marks:
Mark |
Description |
Example |
Mark Properties |
---|---|---|---|
|
A box plot. |
||
|
A continuous band around a line. |
||
|
An errorbar around a point. |
In Altair, marks can be most conveniently specified by the mark_*
methods
of the Chart object, which take optional keyword arguments that are passed to
MarkDef
to configure the look of the marks.
For example, the following uses mark_circle()
with additional
arguments to represent points as red semi-transparent filled circles:
import altair as alt
from vega_datasets import data
url = data.cars.url
alt.Chart(url).mark_circle(
color='red',
opacity=0.3
).encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q'
)
Image Mark#
The image mark, unlike other simple marks, requires the mark to include a
url
encoding, which specifies the PNG to use for the image:
import altair as alt
import pandas as pd
source = pd.DataFrame.from_records([
{"x": 0.5, "y": 0.5, "img": "https://vega.github.io/vega-datasets/data/ffox.png"},
{"x": 1.5, "y": 1.5, "img": "https://vega.github.io/vega-datasets/data/gimp.png"},
{"x": 2.5, "y": 2.5, "img": "https://vega.github.io/vega-datasets/data/7zip.png"}
])
alt.Chart(source).mark_image(
width=50,
height=50
).encode(
x='x',
y='y',
url='img'
)
Compound Marks#
BoxPlot#
The compound mark mark_boxplot()
can be used to create a boxplot without having to specify each part of the plot (box, whiskers, outliers) separately.
import altair as alt
from vega_datasets import data
source = data.population.url
alt.Chart(source).mark_boxplot().encode(
y='people:Q'
).properties(
width=200,
height=300
)
To create a side-by-side boxplot, simply encode the group column on the other axis.
import altair as alt
from vega_datasets import data
source = data.population.url
alt.Chart(source).mark_boxplot().encode(
x='age:O',
y='people:Q'
)
Note that the default behavior is to display outliers as points, where an outlier is defined as any point more than 1.5 IQRs from the box.
Users can adjust this threshold using the extent
property of the mark.
import altair as alt
from vega_datasets import data
source = data.population.url
alt.Chart(source).mark_boxplot(extent=3.0).encode(
x='age:O',
y='people:Q'
)
The outliers can be ignored completely using extent='min-max'
import altair as alt
from vega_datasets import data
source = data.population.url
alt.Chart(source).mark_boxplot(extent='min-max').encode(
x='age:O',
y='people:Q'
)
Mark Properties#
As seen in the last two examples, additional arguments to mark_*()
methods are passed along to an
associated MarkDef
instance, which supports the following attributes:
Property |
Type |
Description |
---|---|---|
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. |
|
angle |
anyOf( |
|
aria |
anyOf( |
|
ariaRole |
anyOf( |
|
ariaRoleDescription |
anyOf( |
|
aspect |
anyOf( |
|
bandSize |
|
The width of the ticks. Default value: 3/4 of step (width step for horizontal ticks and height step for vertical ticks). |
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. |
binSpacing |
|
Offset between bars for binned field. The ideal value for this is either 0 (preferred by statisticians) or 1 (Vega-Lite default, D3 example style). Default value: |
blend |
||
clip |
|
Whether a mark be clipped to the enclosing group’s width and height. |
color |
Default color. Default value: ■ Note:
|
|
continuousBandSize |
|
The default size of the bars on continuous scales. Default value: |
cornerRadius |
anyOf( |
|
cornerRadiusBottomLeft |
anyOf( |
|
cornerRadiusBottomRight |
anyOf( |
|
cornerRadiusEnd |
anyOf( |
|
cornerRadiusTopLeft |
anyOf( |
|
cornerRadiusTopRight |
anyOf( |
|
cursor |
||
description |
anyOf( |
|
dir |
anyOf( |
|
discreteBandSize |
anyOf( |
The default size of the bars with discrete dimensions. If unspecified, the default size is |
dx |
anyOf( |
|
dy |
anyOf( |
|
ellipsis |
anyOf( |
|
fill |
Default fill color. This property has higher precedence than Default value: (None) |
|
fillOpacity |
anyOf( |
|
filled |
|
Whether the mark’s color should be used as fill color instead of stroke color. Default value: Note: This property cannot be used in a style config. |
font |
anyOf( |
|
fontSize |
anyOf( |
|
fontStyle |
||
fontWeight |
anyOf( |
|
height |
anyOf( |
Height of the marks. One of:
|
href |
||
innerRadius |
anyOf( |
The inner radius in pixels of arc marks. Default value: |
interpolate |
anyOf( |
|
invalid |
[‘filter’, None] |
Defines how Vega-Lite should handle marks for invalid values (
|
limit |
anyOf( |
|
line |
anyOf( |
A flag for overlaying line on top of area marks, or an object defining the properties of the overlayed lines.
Default value: |
lineBreak |
anyOf( |
|
lineHeight |
anyOf( |
|
opacity |
anyOf( |
The overall opacity (value between [0,1]). Default value: |
order |
[null, boolean] |
For line and trail marks, this |
orient |
The orientation of a non-stacked bar, tick, area, and line charts. The value is either horizontal (default) or vertical.
|
|
outerRadius |
anyOf( |
The outer radius in pixels of arc marks. Default value: |
padAngle |
anyOf( |
|
point |
anyOf( |
A flag for overlaying points on top of line or area marks, or an object defining the properties of the overlayed points.
Default value: |
radius |
anyOf( |
For arc mark, the primary (outer) radius in pixels. For text marks, polar coordinate radial offset, in pixels, of the text from the origin determined by the Default value: |
radius2 |
anyOf( |
The secondary (inner) radius in pixels of arc marks. Default value: |
radius2Offset |
anyOf( |
Offset for radius2. |
radiusOffset |
anyOf( |
Offset for radius. |
shape |
anyOf(anyOf( |
|
size |
anyOf( |
Default size for marks.
Default value:
|
smooth |
anyOf( |
|
stroke |
Default stroke color. This property has higher precedence than Default value: (None) |
|
strokeCap |
||
strokeDash |
anyOf(array( |
|
strokeDashOffset |
anyOf( |
|
strokeJoin |
anyOf( |
|
strokeMiterLimit |
anyOf( |
|
strokeOffset |
anyOf( |
|
strokeOpacity |
anyOf( |
|
strokeWidth |
anyOf( |
|
style |
anyOf( |
A string or array of strings indicating the name of custom styles to apply to the mark. A style is a named collection of mark property defaults defined within the style configuration. If style is an array, later styles will override earlier styles. Any mark properties explicitly defined within the Default value: The mark’s name. For example, a bar mark will have style |
tension |
anyOf( |
|
text |
||
theta |
anyOf( |
|
theta2 |
anyOf( |
The end angle of arc marks in radians. A value of 0 indicates up or “north”, increasing values proceed clockwise. |
theta2Offset |
anyOf( |
Offset for theta2. |
thetaOffset |
anyOf( |
Offset for theta. |
thickness |
|
Thickness of the tick mark. Default value: |
timeUnitBandPosition |
|
Default relative band position for a time unit. If set to |
timeUnitBandSize |
|
Default relative band size for a time unit. If set to |
tooltip |
anyOf( |
The tooltip text string to show upon mouse hover or an object defining which fields should the tooltip be derived from.
See the Default value: |
type |
The mark type. This could a primitive mark type (one of |
|
url |
||
width |
anyOf( |
Width of the marks. One of:
|
x |
anyOf( |
X coordinates of the marks, or width of horizontal The |
x2 |
anyOf( |
X2 coordinates for ranged The |
x2Offset |
anyOf( |
Offset for x2-position. |
xOffset |
anyOf( |
Offset for x-position. |
y |
anyOf( |
Y coordinates of the marks, or height of vertical The |
y2 |
anyOf( |
Y2 coordinates for ranged The |
y2Offset |
anyOf( |
Offset for y2-position. |
yOffset |
anyOf( |
Offset for y-position. |
Marks can also be configured globally using chart-level configurations; see Mark and Mark Style Configuration for details.