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:
Mark Name |
Method |
Description |
Example |
---|---|---|---|
area |
A filled area plot. |
||
bar |
A bar plot. |
||
circle |
A scatter plot with filled circles. |
||
geoshape |
A geographic shape |
||
image |
A scatter plot with image markers. |
||
line |
A line plot. |
||
point |
A scatter plot with configurable point shapes. |
||
rect |
A filled rectangle, used for heatmaps |
||
rule |
A vertical or horizontal line spanning the axis. |
||
square |
A scatter plot with filled squares. |
N/A |
|
text |
A scatter plot with points represented by text. |
||
tick |
A vertical or horizontal tick mark. |
In addition, Altair provides the following compound marks:
Mark Name |
Method |
Description |
Example |
---|---|---|---|
box plot |
A box plot. |
||
error band |
A continuous band around a line. |
||
error bar |
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='max-min'
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 |
|
angle |
|
The rotation angle of the text, in degrees. |
aspect |
|
Whether to keep aspect ratio of image marks. |
baseline |
The vertical text baseline. One of |
|
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 |
The color blend mode for drawing an item on its current background. Any valid CSS mix-blend-mode value can be used. __Default value: |
|
clip |
|
Whether a mark be clipped to the enclosing group’s width and height. |
color |
Default color. Default value: ■ Note:
|
|
cornerRadius |
|
The radius in pixels of rounded rectangle corners. Default value: |
cornerRadiusBottomLeft |
|
The radius in pixels of rounded rectangle bottom left corner. Default value: |
cornerRadiusBottomRight |
|
The radius in pixels of rounded rectangle bottom right corner. Default value: |
cornerRadiusEnd |
|
|
cornerRadiusTopLeft |
|
The radius in pixels of rounded rectangle top right corner. Default value: |
cornerRadiusTopRight |
|
The radius in pixels of rounded rectangle top left corner. Default value: |
cursor |
The mouse cursor used over the mark. Any valid CSS cursor type can be used. |
|
dir |
The direction of the text. One of Default value: |
|
dx |
|
The horizontal offset, in pixels, between the text label and its anchor point. The offset is applied after rotation by the angle property. |
dy |
|
The vertical offset, in pixels, between the text label and its anchor point. The offset is applied after rotation by the angle property. |
ellipsis |
|
The ellipsis string for text truncated in response to the limit parameter. Default value: |
fill |
Default Fill Color. This property has higher precedence than Default value: (None) |
|
fillOpacity |
|
The fill opacity (value between [0,1]). Default value: |
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 |
|
The typeface to set the text in (e.g., |
fontSize |
|
The font size, in pixels. Default value: |
fontStyle |
The font style (e.g., |
|
fontWeight |
The font weight.
This can be either a string (e.g |
|
height |
|
Height of the marks. |
href |
|
A URL to load upon mouse click. If defined, the mark acts as a hyperlink. |
interpolate |
The line interpolation method to use for line and area marks. One of the following:
|
|
invalid |
[‘filter’, None] |
Defines how Vega-Lite should handle marks for invalid values (
|
limit |
|
The maximum length of the text mark in pixels. The text value will be automatically truncated if the rendered size exceeds the limit. Default value: |
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 |
|
A delimiter, such as a newline character, upon which to break text strings into multiple lines. This property is ignored if the text is array-valued. |
lineHeight |
|
The line height in pixels (the spacing between subsequent lines of text) for multi-line text marks. |
opacity |
|
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.
|
|
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 |
|
Polar coordinate radial offset, in pixels, of the text label from the origin determined by the |
shape |
anyOf( |
Shape of the point marks. Supported values include:
Default value: |
size |
|
Default size for marks.
Default value:
|
stroke |
Default Stroke Color. This property has higher precedence than Default value: (None) |
|
strokeCap |
|
The stroke cap for line ending style. One of Default value: |
strokeDash |
array( |
An array of alternating stroke, space lengths for creating dashed or dotted lines. |
strokeDashOffset |
|
The offset (in pixels) into which to begin drawing with the stroke dash array. |
strokeJoin |
|
The stroke line join method. One of Default value: |
strokeMiterLimit |
|
The miter limit at which to bevel a line join. |
strokeOffset |
|
The offset in pixels at which to draw the group stroke and fill. If unspecified, the default behavior is to dynamically offset stroked groups such that 1 pixel stroke widths align with the pixel grid. |
strokeOpacity |
|
The stroke opacity (value between [0,1]). Default value: |
strokeWidth |
|
The stroke width, in pixels. |
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 |
|
Depending on the interpolation type, sets the tension parameter (for line and area marks). |
text |
Placeholder text if the |
|
theta |
|
Polar coordinate angle, in radians, of the text label from the origin determined by the |
thickness |
|
Thickness of the tick mark. Default value: |
timeUnitBand |
|
Default relative band size for a time unit. If set to |
timeUnitBandPosition |
|
Default relative band position 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 |
|
width |
|
Width of the marks. |
x |
anyOf( |
X coordinates of the marks, or width of horizontal The |
x2 |
anyOf( |
X2 coordinates for ranged The |
x2Offset |
|
Offset for x2-position. |
xOffset |
|
Offset for x-position. |
y |
anyOf( |
Y coordinates of the marks, or height of vertical The |
y2 |
anyOf( |
Y2 coordinates for ranged The |
y2Offset |
|
Offset for y2-position. |
yOffset |
|
Offset for y-position. |
Marks can also be configured globally using chart-level configurations; see Mark and Mark Style Configuration for details.