Top-Level Chart Configuration¶
Many aspects of a chart’s appearance can be configured at the top level using
the configure_*()
methods.
These methods and the properties that they set are only valid at the top level
of a chart, and can be thought of as a way of setting a chart theme: that is,
they set the default styles for the entire chart, and these defaults can be
overridden by specific style settings associated with chart elements.
These methods and their arguments will be outlined below:
For more discussion of approaches to chart customization, see Customizing Visualizations.
Chart Configuration¶
The Chart.configure()
method adds a Config
instance to the chart,
and has the following attributes:
Property |
Type |
Description |
---|---|---|
area |
Area-Specific Config |
|
autosize |
anyOf( |
How the visualization size should be determined. If a string, should be one of Default value: |
axis |
Axis configuration, which determines default properties for all |
|
axisBand |
Config for axes with “band” scales. |
|
axisBottom |
Config for x-axis along the bottom edge of the chart. |
|
axisDiscrete |
Config for axes with “point” or “band” scales. |
|
axisLeft |
Config for y-axis along the left edge of the chart. |
|
axisPoint |
Config for axes with “point” scales. |
|
axisQuantitative |
Config for quantitative axes. |
|
axisRight |
Config for y-axis along the right edge of the chart. |
|
axisTemporal |
Config for temporal axes. |
|
axisTop |
Config for x-axis along the top edge of the chart. |
|
axisX |
X-axis specific config. |
|
axisXBand |
Config for x-axes with “band” scales. |
|
axisXDiscrete |
Config for x-axes with “point” or “band” scales. |
|
axisXPoint |
Config for x-axes with “point” scales. |
|
axisXQuantitative |
Config for x-quantitative axes. |
|
axisXTemporal |
Config for x-temporal axes. |
|
axisY |
Y-axis specific config. |
|
axisYBand |
Config for y-axes with “band” scales. |
|
axisYDiscrete |
Config for y-axes with “point” or “band” scales. |
|
axisYPoint |
Config for y-axes with “point” scales. |
|
axisYQuantitative |
Config for y-quantitative axes. |
|
axisYTemporal |
Config for y-temporal axes. |
|
background |
CSS color property to use as the background of the entire view. Default value: |
|
bar |
Bar-Specific Config |
|
boxplot |
Box Config |
|
circle |
Circle-Specific Config |
|
concat |
Default configuration for all concatenation and repeat view composition operators ( |
|
countTitle |
|
Default axis and legend title for count fields. Default value: |
errorband |
ErrorBand Config |
|
errorbar |
ErrorBar Config |
|
facet |
Default configuration for the |
|
fieldTitle |
[‘verbal’, ‘functional’, ‘plain’] |
Defines how Vega-Lite generates title for fields. There are three possible styles:
|
font |
|
Default font for all text marks, titles, and labels. |
geoshape |
Geoshape-Specific Config |
|
header |
Header configuration, which determines default properties for all headers. For a full list of header configuration options, please see the corresponding section of in the header documentation. |
|
headerColumn |
Header configuration, which determines default properties for column headers. For a full list of header configuration options, please see the corresponding section of in the header documentation. |
|
headerFacet |
Header configuration, which determines default properties for non-row/column facet headers. For a full list of header configuration options, please see the corresponding section of in the header documentation. |
|
headerRow |
Header configuration, which determines default properties for row headers. For a full list of header configuration options, please see the corresponding section of in the header documentation. |
|
image |
Image-specific Config |
|
legend |
Legend configuration, which determines default properties for all legends. For a full list of legend configuration options, please see the corresponding section of in the legend documentation. |
|
line |
Line-Specific Config |
|
lineBreak |
|
A delimiter, such as a newline character, upon which to break text strings into multiple lines. This property provides a global default for text marks, which is overridden by mark or style config settings, and by the lineBreak mark encoding channel. If signal-valued, either string or regular expression (regexp) values are valid. |
mark |
Mark Config |
|
numberFormat |
|
D3 Number format for guide labels and text marks. For example |
padding |
The default visualization padding, in pixels, from the edge of the visualization canvas to the data rectangle. If a number, specifies padding for all sides.
If an object, the value should have the format Default value: |
|
point |
Point-Specific Config |
|
projection |
Projection configuration, which determines default properties for all projections. For a full list of projection configuration options, please see the corresponding section of the projection documentation. |
|
range |
An object hash that defines default range arrays or schemes for using with scales. For a full list of scale range configuration options, please see the corresponding section of the scale documentation. |
|
rect |
Rect-Specific Config |
|
rule |
Rule-Specific Config |
|
scale |
Scale configuration determines default properties for all scales. For a full list of scale configuration options, please see the corresponding section of the scale documentation. |
|
selection |
An object hash for defining default properties for each type of selections. |
|
square |
Square-Specific Config |
|
style |
An object hash that defines key-value mappings to determine default properties for marks with a given style. The keys represent styles names; the values have to be valid mark configuration objects. |
|
text |
Text-Specific Config |
|
tick |
Tick-Specific Config |
|
timeFormat |
|
Default time format for raw time values (without time units) in text marks, legend labels and header labels. Default value: |
title |
Title configuration, which determines default properties for all titles. For a full list of title configuration options, please see the corresponding section of the title documentation. |
|
trail |
Trail-Specific Config |
|
view |
Default properties for single view plots. |
Axis Configuration¶
Axis configuration defines default settings for axes, and can be set using
the Chart.configure_axis()
method.
Properties defined here are applied to all axes in the figure.
Additional property blocks can target more specific axis types based on the orientation (“axisX”, “axisY”, “axisLeft”, “axisTop”, etc.) or band scale type (“axisBand”). For example, properties defined under the “axisBand” property will only apply to axes visualizing “band” scales. If multiple axis config blocks apply to a single axis, type-based options take precedence over orientation-based options, which in turn take precedence over general options.
The methods are the following:
They have the following properties:
Property |
Type |
Description |
---|---|---|
bandPosition |
|
An interpolation fraction indicating where, for Default value: |
disable |
|
Disable axis by default. |
domain |
|
A boolean flag indicating if the domain (the axis baseline) should be included as part of the axis. Default value: |
domainColor |
anyOf( |
Color of axis domain line. Default value: |
domainDash |
array( |
An array of alternating [stroke, space] lengths for dashed domain lines. |
domainDashOffset |
|
The pixel offset at which to start drawing with the domain dash array. |
domainOpacity |
|
Opacity of the axis domain line. |
domainWidth |
|
Stroke width of axis domain line Default value: |
grid |
|
A boolean flag indicating if grid lines should be included as part of the axis Default value: |
gridColor |
anyOf(anyOf( |
|
gridDash |
anyOf(array( |
|
gridDashOffset |
anyOf( |
|
gridOpacity |
anyOf( |
|
gridWidth |
anyOf( |
|
labelAlign |
anyOf( |
|
labelAngle |
|
The rotation angle of the axis labels. Default value: |
labelBaseline |
||
labelBound |
[number, boolean] |
Indicates if labels should be hidden if they exceed the axis range. If Default value: |
labelColor |
anyOf(anyOf( |
|
labelExpr |
|
Vega expression for customizing labels. Note: The label text and value can be assessed via the |
labelFlush |
[boolean, number] |
Indicates if the first and last axis labels should be aligned flush with the scale range. Flush alignment for a horizontal axis will left-align the first label and right-align the last label. For vertical axes, bottom and top text baselines are applied instead. If this property is a number, it also indicates the number of pixels by which to offset the first and last labels; for example, a value of 2 will flush-align the first and last labels and also push them 2 pixels outward from the center of the axis. The additional adjustment can sometimes help the labels better visually group with corresponding axis ticks. Default value: |
labelFlushOffset |
|
Indicates the number of pixels by which to offset flush-adjusted labels. For example, a value of Default value: |
labelFont |
anyOf( |
|
labelFontSize |
anyOf( |
|
labelFontStyle |
||
labelFontWeight |
||
labelLimit |
|
Maximum allowed pixel width of axis tick labels. Default value: |
labelLineHeight |
|
Line height in pixels for multi-line label text. |
labelOffset |
anyOf( |
|
labelOpacity |
anyOf( |
|
labelOverlap |
The strategy to use for resolving overlap of axis labels. If Default value: |
|
labelPadding |
anyOf( |
|
labelSeparation |
|
The minimum separation that must be between label bounding boxes for them to be considered non-overlapping (default |
labels |
|
A boolean flag indicating if labels should be included as part of the axis. Default value: |
maxExtent |
|
The maximum extent in pixels that axis ticks and labels should use. This determines a maximum offset value for axis titles. Default value: |
minExtent |
|
The minimum extent in pixels that axis ticks and labels should use. This determines a minimum offset value for axis titles. Default value: |
offset |
|
The offset, in pixels, by which to displace the axis from the edge of the enclosing group or data rectangle. Default value: derived from the axis config’s |
orient |
The orientation of the axis. One of Default value: |
|
style |
anyOf( |
A string or array of strings indicating the name of custom styles to apply to the axis. A style is a named collection of axis property defined within the style configuration. If style is an array, later styles will override earlier styles. Default value: (none)
Note: Any specified style will augment the default style. For example, an x-axis mark with |
tickBand |
[‘center’, ‘extent’] |
For band scales, indicates if ticks and grid lines should be placed at the center of a band (default) or at the band extents to indicate intervals. |
tickColor |
anyOf(anyOf( |
|
tickCount |
|
A desired number of ticks, for axes visualizing quantitative scales. The resulting number may be different so that values are “nice” (multiples of 2, 5, 10) and lie within the underlying scale’s range. Default value: Determine using a formula |
tickDash |
anyOf(array( |
|
tickDashOffset |
anyOf( |
|
tickExtra |
|
Boolean flag indicating if an extra axis tick should be added for the initial position of the axis. This flag is useful for styling axes for |
tickOffset |
|
Position offset in pixels to apply to ticks, labels, and gridlines. |
tickOpacity |
anyOf( |
|
tickRound |
|
Boolean flag indicating if pixel position values should be rounded to the nearest integer. Default value: |
tickSize |
anyOf( |
|
tickWidth |
anyOf( |
|
ticks |
|
Boolean value that determines whether the axis should include ticks. Default value: |
title |
|
Set to null to disable title for the axis, legend, or header. |
titleAlign |
Horizontal text alignment of axis titles. |
|
titleAnchor |
Text anchor position for placing axis titles. |
|
titleAngle |
|
Angle in degrees of axis titles. |
titleBaseline |
Vertical text baseline for axis titles. |
|
titleColor |
anyOf( |
Color of the title, can be in hex color code or regular color name. |
titleFont |
|
Font of the title. (e.g., |
titleFontSize |
|
Font size of the title. |
titleFontStyle |
Font style of the title. |
|
titleFontWeight |
Font weight of the title.
This can be either a string (e.g |
|
titleLimit |
|
Maximum allowed pixel width of axis titles. |
titleLineHeight |
|
Line height in pixels for multi-line title text. |
titleOpacity |
|
Opacity of the axis title. |
titlePadding |
|
The padding, in pixels, between title and axis. |
titleX |
|
X-coordinate of the axis title relative to the axis group. |
titleY |
|
Y-coordinate of the axis title relative to the axis group. |
translate |
|
Translation offset in pixels applied to the axis group mark x and y. If specified, overrides the default behavior of a 0.5 offset to pixel-align stroked lines. |
Header Configuration¶
The Chart.configure_header()
method allows configuration of facet headers,
including the font, color, size, and position of the title and labels.
Here is an example:
import altair as alt
from vega_datasets import data
source = data.cars.url
chart = alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color='Origin:N',
column='Origin:N'
).properties(
width=180,
height=180
)
chart.configure_header(
titleColor='green',
titleFontSize=14,
labelColor='red',
labelFontSize=14
)
Property |
Type |
Description |
---|---|---|
format |
anyOf( |
When used with the default
See the format documentation for more examples. When used with a custom Default value: Derived from numberFormat config for number format and from timeFormat config for time format. |
formatType |
|
The format type for labels ( Default value:
|
labelAlign |
Horizontal text alignment of header labels. One of |
|
labelAnchor |
The anchor position for placing the labels. One of |
|
labelAngle |
|
The rotation angle of the header labels. Default value: |
labelBaseline |
The vertical text baseline for the header labels. One of |
|
labelColor |
The color of the header label, can be in hex color code or regular color name. |
|
labelExpr |
|
Vega expression for customizing labels. Note: The label text and value can be assessed via the |
labelFont |
|
The font of the header label. |
labelFontSize |
|
The font size of the header label, in pixels. |
labelFontStyle |
The font style of the header label. |
|
labelFontWeight |
The font weight of the header label. |
|
labelLimit |
|
The maximum length of the header label in pixels. The text value will be automatically truncated if the rendered size exceeds the limit. Default value: |
labelLineHeight |
|
Line height in pixels for multi-line header labels or title text with |
labelOrient |
The orientation of the header label. One of |
|
labelPadding |
|
The padding, in pixel, between facet header’s label and the plot. Default value: |
labels |
|
A boolean flag indicating if labels should be included as part of the header. Default value: |
orient |
Shortcut for setting both labelOrient and titleOrient. |
|
title |
|
Set to null to disable title for the axis, legend, or header. |
titleAlign |
Horizontal text alignment (to the anchor) of header titles. |
|
titleAnchor |
The anchor position for placing the title. One of |
|
titleAngle |
|
The rotation angle of the header title. Default value: |
titleBaseline |
The vertical text baseline for the header title. One of Default value: |
|
titleColor |
Color of the header title, can be in hex color code or regular color name. |
|
titleFont |
|
Font of the header title. (e.g., |
titleFontSize |
|
Font size of the header title. |
titleFontStyle |
The font style of the header title. |
|
titleFontWeight |
Font weight of the header title.
This can be either a string (e.g |
|
titleLimit |
|
The maximum length of the header title in pixels. The text value will be automatically truncated if the rendered size exceeds the limit. Default value: |
titleLineHeight |
|
Line height in pixels for multi-line header title text or title text with |
titleOrient |
The orientation of the header title. One of |
|
titlePadding |
|
The padding, in pixel, between facet header’s title and the label. Default value: |
Legend Configuration¶
The Chart.configure_legend()
allows you to customize the appearance of chart
legends, including location, fonts, bounding boxes, colors, and more.
Here is an example:
import altair as alt
from vega_datasets import data
source = data.cars.url
chart = alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color='Origin:N'
)
chart.configure_legend(
strokeColor='gray',
fillColor='#EEEEEE',
padding=10,
cornerRadius=10,
orient='top-right'
)
Additional properties are summarized in the following table:
Property |
Type |
Description |
---|---|---|
clipHeight |
|
The height in pixels to clip symbol legend entries and limit their size. |
columnPadding |
|
The horizontal padding in pixels between symbol legend entries. Default value: |
columns |
|
The number of columns in which to arrange symbol legend entries. A value of |
cornerRadius |
|
Corner radius for the full legend. |
direction |
The direction of the legend, one of Default value:
|
|
disable |
|
Disable legend by default |
fillColor |
anyOf( |
Background fill color for the full legend. |
gradientDirection |
The default direction ( Default value: |
|
gradientHorizontalMaxLength |
|
Max legend length for a horizontal gradient when Default value: |
gradientHorizontalMinLength |
|
Min legend length for a horizontal gradient when Default value: |
gradientLabelLimit |
|
The maximum allowed length in pixels of color ramp gradient labels. |
gradientLabelOffset |
|
Vertical offset in pixels for color ramp gradient labels. Default value: |
gradientLength |
|
The length in pixels of the primary axis of a color gradient. This value corresponds to the height of a vertical gradient or the width of a horizontal gradient. Default value: |
gradientOpacity |
|
Opacity of the color gradient. |
gradientStrokeColor |
anyOf( |
The color of the gradient stroke, can be in hex color code or regular color name. Default value: |
gradientStrokeWidth |
|
The width of the gradient stroke, in pixels. Default value: |
gradientThickness |
|
The thickness in pixels of the color gradient. This value corresponds to the width of a vertical gradient or the height of a horizontal gradient. Default value: |
gradientVerticalMaxLength |
|
Max legend length for a vertical gradient when Default value: |
gradientVerticalMinLength |
|
Min legend length for a vertical gradient when Default value: |
gridAlign |
The alignment to apply to symbol legends rows and columns. The supported string values are Default value: |
|
labelAlign |
The alignment of the legend label, can be left, center, or right. |
|
labelBaseline |
The position of the baseline of legend label, can be Default value: |
|
labelColor |
anyOf( |
The color of the legend label, can be in hex color code or regular color name. |
labelFont |
|
The font of the legend label. |
labelFontSize |
|
The font size of legend label. Default value: |
labelFontStyle |
The font style of legend label. |
|
labelFontWeight |
The font weight of legend label. |
|
labelLimit |
|
Maximum allowed pixel width of legend tick labels. Default value: |
labelOffset |
|
The offset of the legend label. |
labelOpacity |
|
Opacity of labels. |
labelOverlap |
The strategy to use for resolving overlap of labels in gradient legends. If Default value: |
|
labelPadding |
|
Padding in pixels between the legend and legend labels. |
labelSeparation |
|
The minimum separation that must be between label bounding boxes for them to be considered non-overlapping (default |
layout |
– |
Legend orient group layout parameters. |
legendX |
|
Custom x-position for legend with orient “none”. |
legendY |
|
Custom y-position for legend with orient “none”. |
offset |
|
The offset in pixels by which to displace the legend from the data rectangle and axes. Default value: |
orient |
The orientation of the legend, which determines how the legend is positioned within the scene. One of Default value: |
|
padding |
|
The padding between the border and content of the legend group. Default value: |
rowPadding |
|
The vertical padding in pixels between symbol legend entries. Default value: |
strokeColor |
anyOf( |
Border stroke color for the full legend. |
strokeDash |
array( |
Border stroke dash pattern for the full legend. |
strokeWidth |
|
Border stroke width for the full legend. |
symbolBaseFillColor |
anyOf( |
Default fill color for legend symbols. Only applied if there is no Default value: |
symbolBaseStrokeColor |
anyOf( |
Default stroke color for legend symbols. Only applied if there is no Default value: |
symbolDash |
array( |
An array of alternating [stroke, space] lengths for dashed symbol strokes. |
symbolDashOffset |
|
The pixel offset at which to start drawing with the symbol stroke dash array. |
symbolDirection |
The default direction ( Default value: |
|
symbolFillColor |
anyOf( |
The color of the legend symbol, |
symbolLimit |
|
The maximum number of allowed entries for a symbol legend. Additional entries will be dropped. |
symbolOffset |
|
Horizontal pixel offset for legend symbols. Default value: |
symbolOpacity |
|
Opacity of the legend symbols. |
symbolSize |
|
The size of the legend symbol, in pixels. Default value: |
symbolStrokeColor |
anyOf( |
Stroke color for legend symbols. |
symbolStrokeWidth |
|
The width of the symbol’s stroke. Default value: |
symbolType |
The symbol shape. One of the plotting shapes Default value: |
|
tickCount |
The desired number of tick values for quantitative legends. |
|
title |
|
Set to null to disable title for the axis, legend, or header. |
titleAlign |
Horizontal text alignment for legend titles. Default value: |
|
titleAnchor |
Text anchor position for placing legend titles. |
|
titleBaseline |
Vertical text baseline for legend titles. Default value: |
|
titleColor |
anyOf( |
The color of the legend title, can be in hex color code or regular color name. |
titleFont |
|
The font of the legend title. |
titleFontSize |
|
The font size of the legend title. |
titleFontStyle |
The font style of the legend title. |
|
titleFontWeight |
The font weight of the legend title.
This can be either a string (e.g |
|
titleLimit |
|
Maximum allowed pixel width of legend titles. Default value: |
titleLineHeight |
|
Line height in pixels for multi-line title text. |
titleOpacity |
|
Opacity of the legend title. |
titleOrient |
Orientation of the legend title. |
|
titlePadding |
|
The padding, in pixels, between title and legend. Default value: |
unselectedOpacity |
|
The opacity of unselected legend entries. Default value: 0.35. |
Mark and Mark Style Configuration¶
The mark configuration can be set using the Chart.configure_mark()
method, which sets the default properties for all marks in the chart.
In addition, the config object also provides mark-specific configuration
using the mark type (e.g. Chart.configure_area()
) for
defining default properties for each mark.
For general configuration of all mark types, use:
For configurations specific to particular mark types, use:
Each of the above methods accepts the following properties:
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 |
|
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: |
|
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: |
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: |
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.
|
|
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. |
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 |
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: |
width |
|
Width of the marks. |
x |
anyOf( |
X coordinates of the marks, or width of horizontal The |
x2 |
anyOf( |
X2 coordinates for ranged The |
y |
anyOf( |
Y coordinates of the marks, or height of vertical The |
y2 |
anyOf( |
Y2 coordinates for ranged The |
In addition to the default mark properties above, default values can be
further customized using named styles defined as keyword arguments to
the Chart.configure_style()
method.
Styles can then be invoked by including a style property within a mark
definition object.
Scale Configuration¶
Scales can be configured using Chart.configure_scale()
, which has
the following properties:
Property |
Type |
Description |
---|---|---|
bandPaddingInner |
|
Default inner padding for Default value:
|
bandPaddingOuter |
|
Default outer padding for Default value: |
barBandPaddingInner |
|
Default inner padding for Default value: |
clamp |
|
If true, values that exceed the data domain are clamped to either the minimum or maximum range value |
continuousPadding |
|
Default padding for continuous scales. Default: |
maxBandSize |
|
The default max value for mapping quantitative fields to bar’s size/bandSize. If undefined (default), we will use the axis’s size (width or height) - 1. |
maxFontSize |
|
The default max value for mapping quantitative fields to text’s size/fontSize. Default value: |
maxOpacity |
|
Default max opacity for mapping a field to opacity. Default value: |
maxSize |
|
Default max value for point size scale. |
maxStrokeWidth |
|
Default max strokeWidth for the scale of strokeWidth for rule and line marks and of size for trail marks. Default value: |
minBandSize |
|
The default min value for mapping quantitative fields to bar and tick’s size/bandSize scale with zero=false. Default value: |
minFontSize |
|
The default min value for mapping quantitative fields to tick’s size/fontSize scale with zero=false Default value: |
minOpacity |
|
Default minimum opacity for mapping a field to opacity. Default value: |
minSize |
|
Default minimum value for point size scale with zero=false. Default value: |
minStrokeWidth |
|
Default minimum strokeWidth for the scale of strokeWidth for rule and line marks and of size for trail marks with zero=false. Default value: |
pointPadding |
|
Default outer padding for Default value: |
quantileCount |
|
Default range cardinality for Default value: |
quantizeCount |
|
Default range cardinality for Default value: |
rectBandPaddingInner |
|
Default inner padding for Default value: |
round |
|
If true, rounds numeric output values to integers.
This can be helpful for snapping to the pixel grid.
(Only available for |
useUnaggregatedDomain |
|
Use the source data range before aggregation as scale domain instead of aggregated data for aggregate axis. This is equivalent to setting This property only works with aggregate functions that produce values within the raw data domain ( Default value: |
xReverse |
|
Reverse x-scale by default (useful for right-to-left charts). |
Scale Range Configuration¶
Scale ranges can be configured using Chart.configure_range()
, which has
the following properties:
Property |
Type |
Description |
---|---|---|
category |
anyOf( |
Default color scheme for categorical data. |
diverging |
anyOf( |
Default color scheme for diverging quantitative ramps. |
heatmap |
anyOf( |
Default color scheme for quantitative heatmaps. |
ordinal |
anyOf( |
Default color scheme for rank-ordered data. |
ramp |
anyOf( |
Default color scheme for sequential quantitative ramps. |
symbol |
array( |
Array of symbol names or paths for the default shape palette. |
Projection Configuration¶
Property |
Type |
Description |
---|---|---|
center |
|
The projection’s center, a two-element array of longitude and latitude in degrees. Default value: |
clipAngle |
|
The projection’s clipping circle radius to the specified angle in degrees. If |
clipExtent |
|
The projection’s viewport clip extent to the specified bounds in pixels. The extent bounds are specified as an array |
coefficient |
|
|
distance |
|
|
extent |
|
|
fit |
||
fraction |
|
|
lobes |
|
|
parallel |
|
|
parallels |
array( |
For conic projections, the two standard parallels that define the map layout. The default depends on the specific conic projection used. |
pointRadius |
|
The default radius (in pixels) to use when drawing GeoJSON Default value: |
precision |
|
The threshold for the projection’s adaptive resampling to the specified value in pixels. This value corresponds to the Douglas–Peucker distance. If precision is not specified, returns the projection’s current resampling precision which defaults to |
radius |
|
|
ratio |
|
|
reflectX |
|
|
reflectY |
|
|
rotate |
anyOf( |
The projection’s three-axis rotation to the specified angles, which must be a two- or three-element array of numbers [ Default value: |
scale |
|
The projection’s scale (zoom) factor, overriding automatic fitting. The default scale is projection-specific. The scale factor corresponds linearly to the distance between projected points; however, scale factor values are not equivalent across projections. |
size |
|
|
spacing |
|
|
tilt |
|
|
translate |
|
The projection’s translation offset as a two-element array |
type |
The cartographic projection to use. This value is case-insensitive, for example Default value: |
Selection Configuration¶
Property |
Type |
Description |
---|---|---|
interval |
The default definition for an For instance, setting |
|
multi |
The default definition for a For instance, setting |
|
single |
The default definition for a For instance, setting |
Title Configuration¶
The Chart.configure_title()
method allows configuration of the chart
title, including the font, color, placement, and orientation.
Here is an example:
import altair as alt
from vega_datasets import data
source = data.cars.url
chart = alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
).properties(
title='Cars Data'
)
chart.configure_title(
fontSize=20,
font='Courier',
anchor='start',
color='gray'
)
Additional title configuration options are listed in the following table:
Property |
Type |
Description |
---|---|---|
align |
Horizontal text alignment for title text. One of |
|
anchor |
The anchor position for placing the title and subtitle text. One of |
|
angle |
|
Angle in degrees of title and subtitle text. |
baseline |
Vertical text baseline for title and subtitle text. One of |
|
color |
anyOf( |
Text color for title text. |
dx |
|
Delta offset for title and subtitle text x-coordinate. |
dy |
|
Delta offset for title and subtitle text y-coordinate. |
font |
|
Font name for title text. |
fontSize |
|
Font size in pixels for title text. |
fontStyle |
Font style for title text. |
|
fontWeight |
Font weight for title text.
This can be either a string (e.g |
|
frame |
anyOf( |
The reference frame for the anchor position, one of |
limit |
|
The maximum allowed length in pixels of title and subtitle text. |
lineHeight |
|
Line height in pixels for multi-line title text. |
offset |
|
The orthogonal offset in pixels by which to displace the title group from its position along the edge of the chart. |
orient |
Default title orientation ( |
|
subtitleColor |
anyOf( |
Text color for subtitle text. |
subtitleFont |
|
Font name for subtitle text. |
subtitleFontSize |
|
Font size in pixels for subtitle text. |
subtitleFontStyle |
Font style for subtitle text. |
|
subtitleFontWeight |
Font weight for subtitle text.
This can be either a string (e.g |
|
subtitleLineHeight |
|
Line height in pixels for multi-line subtitle text. |
subtitlePadding |
|
The padding in pixels between title and subtitle text. |
View Configuration¶
The Chart.configure_view()
method allows you to configure aspects of the
chart’s view, i.e. the area of the screen in which the data and scales are
drawn. Here is an example to demonstrate some of the visual features that can
be controlled:
import altair as alt
from vega_datasets import data
source = data.cars.url
chart = alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
)
chart.configure_view(
continuousHeight=200,
continuousWidth=200,
strokeWidth=4,
fill='#FFEEDD',
stroke='red',
)
Additional properties are summarized in the following table:
Property |
Type |
Description |
---|---|---|
clip |
|
Whether the view should be clipped. |
continuousHeight |
|
The default height when the plot has a continuous y-field. Default value: |
continuousWidth |
|
The default width when the plot has a continuous x-field. Default value: |
cornerRadius |
|
The radius in pixels of rounded rectangle corners. Default value: |
cursor |
The mouse cursor used over the view. Any valid CSS cursor type can be used. |
|
discreteHeight |
anyOf( |
The default height when the plot has either a discrete y-field or no y-field.
The height can be either a number indicating a fixed height or an object in the form of Default value: a step size based on |
discreteWidth |
anyOf( |
The default width when the plot has either a discrete x-field or no x-field.
The width can be either a number indicating a fixed width or an object in the form of Default value: a step size based on |
fill |
anyOf( |
The fill color. Default value: |
fillOpacity |
|
The fill opacity (value between [0,1]). Default value: |
height |
|
Default height Deprecated: Since Vega-Lite 4.0. Please use continuousHeight and discreteHeight instead. |
opacity |
|
The overall opacity (value between [0,1]). Default value: |
step |
|
Default step size for x-/y- discrete fields. |
stroke |
anyOf( |
The stroke color. Default value: |
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. |
strokeOpacity |
|
The stroke opacity (value between [0,1]). Default value: |
strokeWidth |
|
The stroke width, in pixels. |
width |
|
Default width Deprecated: Since Vega-Lite 4.0. Please use continuousWidth and discreteWidth instead. |
Altair Themes¶
Altair makes available a theme registry that lets users apply chart configurations
globally within any Python session. This is done via the alt.themes
object.
The themes registry consists of functions which define a specification dictionary that will be added to every created chart. For example, the default theme configures the default size of a single chart:
>>> import altair as alt
>>> default = alt.themes.get()
>>> default()
{'config': {'view': {'continuousWidth': 400, 'continuousHeight': 300}}}
You can see that any chart you create will have this theme applied, and these configurations added to its specification:
import altair as alt
from vega_datasets import data
chart = alt.Chart(data.cars.url).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q'
)
chart.to_dict()
{'config': {'view': {'continuousWidth': 400, 'continuousHeight': 300}}, 'data': {'url': 'https://vega.github.io/vega-datasets/data/cars.json'}, 'mark': 'point', 'encoding': {'x': {'type': 'quantitative', 'field': 'Horsepower'}, 'y': {'type': 'quantitative', 'field': 'Miles_per_Gallon'}}, '$schema': 'https://vega.github.io/schema/vega-lite/v4.8.1.json'}
The rendered chart will then reflect these configurations:
chart
Changing the Theme¶
If you would like to enable any other theme for the length of your Python session,
you can call alt.themes.enable(theme_name)
.
For example, Altair includes a theme in which the chart background is opaque
rather than transparent:
alt.themes.enable('opaque')
chart.to_dict()
{'config': {'background': 'white', 'view': {'continuousWidth': 400, 'continuousHeight': 300}}, 'data': {'url': 'https://vega.github.io/vega-datasets/data/cars.json'}, 'mark': 'point', 'encoding': {'x': {'type': 'quantitative', 'field': 'Horsepower'}, 'y': {'type': 'quantitative', 'field': 'Miles_per_Gallon'}}, '$schema': 'https://vega.github.io/schema/vega-lite/v4.8.1.json'}
chart
Notice that the background color of the chart is now set to white.
If you would like no theme applied to your chart, you can use the
theme named 'none'
:
alt.themes.enable('none')
chart.to_dict()
{'data': {'url': 'https://vega.github.io/vega-datasets/data/cars.json'}, 'mark': 'point', 'encoding': {'x': {'type': 'quantitative', 'field': 'Horsepower'}, 'y': {'type': 'quantitative', 'field': 'Miles_per_Gallon'}}, '$schema': 'https://vega.github.io/schema/vega-lite/v4.8.1.json'}
chart
Because the view configuration is not set, the chart is smaller than the default rendering.
If you would like to use any theme just for a single chart, you can use the
with
statement to enable a temporary theme:
with alt.themes.enable('default'):
spec = chart.to_json()
Currently Altair does not offer many built-in themes, but we plan to add more options in the future.
Defining a Custom Theme¶
The theme registry also allows defining and registering custom themes. A theme is simply a function that returns a dictionary of default values to be added to the chart specification at rendering time, which is then registered and activated.
For example, here we define a theme in which all marks are drawn with black fill unless otherwise specified:
import altair as alt
from vega_datasets import data
# define the theme by returning the dictionary of configurations
def black_marks():
return {
'config': {
'view': {
'height': 300,
'width': 400,
},
'mark': {
'color': 'black',
'fill': 'black'
}
}
}
# register the custom theme under a chosen name
alt.themes.register('black_marks', black_marks)
# enable the newly registered theme
alt.themes.enable('black_marks')
# draw the chart
cars = data.cars.url
alt.Chart(cars).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q'
)
If you want to restore the default theme, use:
alt.themes.enable('default')
For more ideas on themes, see the Vega Themes repository.