CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-altair

Vega-Altair: A declarative statistical visualization library for Python.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

encodings-channels.mddocs/

Encodings & Channels

Visual encoding channels that map data fields to visual properties like position, color, size, and shape. These form the core of Altair's grammar of graphics implementation, allowing declarative mapping from data dimensions to visual dimensions.

Capabilities

Position Channels

Encoding channels for spatial positioning of marks on x and y axes, including primary and secondary positions for ranges and spans.

class X:
    def __init__(
        self,
        field=None,
        type=None,
        aggregate=None,
        bin=None,
        timeUnit=None,
        scale=None,
        axis=None,
        sort=None,
        title=None,
        **kwargs
    ):
        """
        X-axis position encoding.
        
        Parameters:
        - field: Data field name
        - type: Data type ('quantitative', 'ordinal', 'nominal', 'temporal')
        - aggregate: Aggregation function ('mean', 'sum', 'count', etc.)
        - bin: Binning specification (bool or BinParams)
        - timeUnit: Time unit for temporal data
        - scale: Scale configuration
        - axis: Axis configuration
        - sort: Sort specification
        - title: Axis title
        """

class Y:
    def __init__(
        self,
        field=None,
        type=None,
        aggregate=None,
        bin=None,
        timeUnit=None,
        scale=None,
        axis=None,
        sort=None,
        title=None,
        **kwargs
    ):
        """Y-axis position encoding."""

class X2:
    def __init__(self, field=None, type=None, value=None, datum=None, **kwargs):
        """Secondary x-position for spans/ranges."""

class Y2:
    def __init__(self, field=None, type=None, value=None, datum=None, **kwargs):
        """Secondary y-position for spans/ranges."""

class XOffset:
    def __init__(self, field=None, type=None, value=None, datum=None, **kwargs):
        """X-position offset for fine positioning."""

class YOffset:
    def __init__(self, field=None, type=None, value=None, datum=None, **kwargs):
        """Y-position offset for fine positioning."""

Mark Property Channels

Encoding channels for visual properties of marks including color, size, opacity, and stroke properties.

class Color:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        legend=None,
        condition=None,
        value=None,
        **kwargs
    ):
        """
        Color encoding for mark fill/stroke.
        
        Parameters:
        - field: Data field name
        - type: Data type
        - scale: Color scale configuration
        - legend: Legend configuration
        - condition: Conditional encoding
        - value: Fixed color value
        """

class Fill:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        legend=None,
        condition=None,
        value=None,
        **kwargs
    ):
        """Fill color encoding."""

class Stroke:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        legend=None,
        condition=None,
        value=None,
        **kwargs
    ):
        """Stroke color encoding."""

class Opacity:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        legend=None,
        condition=None,
        value=None,
        **kwargs
    ):
        """Opacity encoding."""

class FillOpacity:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        legend=None,
        condition=None,
        value=None,
        **kwargs
    ):
        """Fill opacity encoding."""

class StrokeOpacity:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        legend=None,
        condition=None,
        value=None,
        **kwargs
    ):
        """Stroke opacity encoding."""

class Size:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        legend=None,
        condition=None,
        value=None,
        **kwargs
    ):
        """Size encoding for marks."""

class StrokeWidth:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        legend=None,
        condition=None,
        value=None,
        **kwargs
    ):
        """Stroke width encoding."""

class StrokeDash:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        legend=None,
        condition=None,
        value=None,
        **kwargs
    ):
        """Stroke dash pattern encoding."""

class Shape:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        legend=None,
        condition=None,
        value=None,
        **kwargs
    ):
        """Shape encoding for point marks."""

Polar Coordinate Channels

Encoding channels for polar coordinate systems used in pie charts, arc plots, and radial visualizations.

class Angle:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        value=None,
        **kwargs
    ):
        """Angle encoding for arc marks."""

class Theta:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        value=None,
        **kwargs
    ):
        """Angular position in polar coordinates."""

class Theta2:
    def __init__(self, field=None, type=None, value=None, datum=None, **kwargs):
        """Secondary angular position."""

class Radius:
    def __init__(
        self,
        field=None,
        type=None,
        scale=None,
        value=None,
        **kwargs
    ):
        """Radial position in polar coordinates."""

class Radius2:
    def __init__(self, field=None, type=None, value=None, datum=None, **kwargs):
        """Secondary radial position."""

Facet and Detail Channels

Encoding channels for creating small multiples, grouping data, and controlling visual details without mapping to visual properties.

class Column:
    def __init__(
        self,
        field=None,
        type=None,
        sort=None,
        spacing=None,
        header=None,
        **kwargs
    ):
        """Column faceting encoding."""

class Row:
    def __init__(
        self,
        field=None,
        type=None,
        sort=None,
        spacing=None,
        header=None,
        **kwargs
    ):
        """Row faceting encoding."""

class Facet:
    def __init__(
        self,
        field=None,
        type=None,
        columns=None,
        sort=None,
        header=None,
        **kwargs
    ):
        """General faceting encoding."""

class Detail:
    def __init__(self, field=None, type=None, **kwargs):
        """Additional grouping without visual encoding."""

class Key:
    def __init__(self, field=None, type=None, **kwargs):
        """Key field for object constancy in animations."""

class Order:
    def __init__(self, field=None, type=None, sort=None, **kwargs):
        """Order encoding for line/area connection order."""

Metadata Channels

Encoding channels for metadata and supplementary information like tooltips, hyperlinks, and accessibility descriptions.

class Tooltip:
    def __init__(
        self,
        field=None,
        type=None,
        format=None,
        formatType=None,
        value=None,
        **kwargs
    ):
        """Tooltip encoding for hover information."""

class Href:
    def __init__(self, field=None, type=None, value=None, **kwargs):
        """Hyperlink encoding."""

class Description:
    def __init__(self, field=None, type=None, value=None, **kwargs):
        """Accessibility description encoding."""

class Text:
    def __init__(
        self,
        field=None,
        type=None,
        format=None,
        formatType=None,
        value=None,
        **kwargs
    ):
        """Text encoding for text marks."""

Geographic Channels

Encoding channels specifically for geographic and cartographic data visualization.

class Latitude:
    def __init__(self, field=None, type=None, scale=None, **kwargs):
        """Latitude encoding for geographic data."""

class Longitude:
    def __init__(self, field=None, type=None, scale=None, **kwargs):
        """Longitude encoding for geographic data."""

class Latitude2:
    def __init__(self, field=None, type=None, value=None, datum=None, **kwargs):
        """Secondary latitude coordinate."""

class Longitude2:
    def __init__(self, field=None, type=None, value=None, datum=None, **kwargs):
        """Secondary longitude coordinate."""

Error Bar Channels

Encoding channels for representing uncertainty and error ranges in visualizations.

class XError:
    def __init__(self, field=None, type=None, value=None, **kwargs):
        """X-direction error encoding."""

class YError:
    def __init__(self, field=None, type=None, value=None, **kwargs):
        """Y-direction error encoding."""

class XError2:
    def __init__(self, field=None, type=None, value=None, **kwargs):
        """Secondary X-direction error encoding."""

class YError2:
    def __init__(self, field=None, type=None, value=None, **kwargs):
        """Secondary Y-direction error encoding."""

Usage Examples

Basic Position Encoding

import altair as alt

# Simple x, y encoding
chart = alt.Chart(data).mark_point().encode(
    x='xfield:Q',  # Quantitative x
    y='yfield:Q'   # Quantitative y
)

# With explicit encoding objects
chart = alt.Chart(data).mark_point().encode(
    x=alt.X('xfield:Q', title='X Axis Title'),
    y=alt.Y('yfield:Q', scale=alt.Scale(zero=False))
)

Color and Size Encoding

# Color by category, size by value
chart = alt.Chart(data).mark_circle().encode(
    x='x:Q',
    y='y:Q',
    color=alt.Color('category:N', scale=alt.Scale(scheme='category10')),
    size=alt.Size('value:Q', scale=alt.Scale(range=[50, 400]))
)

Conditional Encoding

# Conditional color based on selection
selection = alt.selection_point()

chart = alt.Chart(data).mark_circle().add_selection(
    selection
).encode(
    x='x:Q',
    y='y:Q',
    color=alt.condition(selection, 'category:N', alt.value('lightgray'))
)

Faceted Visualization

# Facet by category
chart = alt.Chart(data).mark_point().encode(
    x='x:Q',
    y='y:Q',
    column=alt.Column('category:N', header=alt.Header(title='Category'))
)

Tooltip with Multiple Fields

# Multi-field tooltip
chart = alt.Chart(data).mark_circle().encode(
    x='x:Q',
    y='y:Q',
    tooltip=['x:Q', 'y:Q', 'category:N', 'value:Q']
)

# Custom tooltip formatting
chart = alt.Chart(data).mark_circle().encode(
    x='x:Q',
    y='y:Q',
    tooltip=[
        alt.Tooltip('x:Q', title='X Value'),
        alt.Tooltip('y:Q', title='Y Value', format='.2f')
    ]
)

Types

from typing import Union, Dict, Any, Optional, List

# Data types
DataType = Union['quantitative', 'ordinal', 'nominal', 'temporal', 'geojson']
DataTypeShort = Union['Q', 'O', 'N', 'T', 'G']

# Field specification
FieldName = str
ShorthandString = str  # e.g., 'field:Q', 'mean(field):Q'

# Aggregate operations
AggregateOp = Union[
    'argmax', 'argmin', 'average', 'count', 'distinct', 'max', 'mean', 
    'median', 'min', 'missing', 'q1', 'q3', 'ci0', 'ci1', 'stderr', 
    'stdev', 'stdevp', 'sum', 'valid', 'values', 'variance', 'variancep'
]

# Time units
TimeUnit = Union[
    'year', 'quarter', 'month', 'day', 'date', 'hours', 'minutes', 
    'seconds', 'milliseconds', 'yearquarter', 'yearquartermonth',
    'yearmonth', 'yearmonthdate', 'yearmonthdatehours',
    'yearmonthdatehoursminutes', 'yearmonthdatehoursminutesseconds'
]

# Scale types
ScaleType = Union[
    'linear', 'log', 'pow', 'sqrt', 'symlog', 'identity', 'sequential',
    'time', 'utc', 'ordinal', 'band', 'point', 'bin-ordinal', 'quantile',
    'quantize', 'threshold'
]

# Sort specification
SortOrder = Union['ascending', 'descending']
SortField = Dict[str, Any]
Sort = Union[SortOrder, SortField, List[str], None]

# Conditional encoding
ConditionalEncoding = Dict[str, Any]

# Value types for encodings
EncodingValue = Union[str, int, float, bool, None]

Install with Tessl CLI

npx tessl i tessl/pypi-altair

docs

chart-composition.md

chart-creation.md

configuration-theming.md

data-handling.md

encodings-channels.md

expressions-conditions.md

index.md

parameters-interactions.md

rendering-output.md

transformations.md

tile.json