Vega-Altair: A declarative statistical visualization library for Python.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core functionality for creating and configuring charts with data, marks, and encodings. The Chart class is the primary interface for building visualizations in Altair's grammar of graphics approach.
The main Chart class that serves as the foundation for all visualizations. It combines data with mark specifications and encodings to create declarative visualization specifications.
class Chart:
def __init__(
self,
data=None,
encoding=None,
mark=None,
width=None,
height=None,
title=None,
**kwargs
):
"""
Create a new Chart object.
Parameters:
- data: Data source (DataFrame, URL, or inline data)
- encoding: Encoding specification dictionary
- mark: Mark specification (string or MarkDef object)
- width: Chart width in pixels
- height: Chart height in pixels
- title: Chart title
"""
def add_params(self, *params):
"""Add parameters to the chart for interactivity."""
def add_selection(self, *selections):
"""Add selection parameters (deprecated, use add_params)."""
def encode(self, **kwargs):
"""Add encodings to the chart."""
def properties(
self,
width=None,
height=None,
title=None,
background=None,
padding=None,
**kwargs
):
"""Set chart properties like dimensions and title."""
def resolve_axis(self, **kwargs):
"""Resolve axis sharing for composed charts."""
def resolve_legend(self, **kwargs):
"""Resolve legend sharing for composed charts."""
def resolve_scale(self, **kwargs):
"""Resolve scale sharing for composed charts."""
def to_dict(self):
"""Convert chart to Vega-Lite specification dictionary."""
def to_json(self, indent=2, **kwargs):
"""Convert chart to JSON string."""
def save(self, filename, **kwargs):
"""Save chart to file (PNG, SVG, HTML, JSON)."""
def show(self):
"""Display chart in current environment."""
def serve(self, port=8888, open=True):
"""Serve chart in local web server."""
def facet(
self,
facet=None,
column=None,
row=None,
data=None,
columns=None,
spacing=None,
**kwargs
):
"""Create faceted chart with small multiples."""
def to_html(
self,
base_url=None,
embed_options=None,
json_kwds=None,
**kwargs
):
"""Convert chart to HTML string."""
def repeat(self, repeat=None, **kwargs):
"""Create repeated chart specification."""Methods for creating charts with basic mark types. Each method returns a new Chart object with the specified mark.
# Point and scatter marks
def mark_point(
self,
filled=None,
opacity=None,
size=None,
stroke=None,
strokeWidth=None,
**kwargs
):
"""Create point/scatter plot marks."""
def mark_circle(
self,
opacity=None,
size=None,
stroke=None,
strokeWidth=None,
**kwargs
):
"""Create circle marks."""
def mark_square(
self,
opacity=None,
size=None,
stroke=None,
strokeWidth=None,
**kwargs
):
"""Create square marks."""
# Line marks
def mark_line(
self,
interpolate=None,
strokeDash=None,
strokeWidth=None,
opacity=None,
**kwargs
):
"""Create line marks."""
def mark_trail(
self,
size=None,
opacity=None,
**kwargs
):
"""Create trail marks with variable width."""
def mark_rule(
self,
strokeDash=None,
strokeWidth=None,
opacity=None,
**kwargs
):
"""Create rule/line marks."""
# Area marks
def mark_area(
self,
interpolate=None,
opacity=None,
**kwargs
):
"""Create area marks."""
def mark_bar(
self,
opacity=None,
cornerRadius=None,
**kwargs
):
"""Create bar marks."""
def mark_rect(
self,
opacity=None,
cornerRadius=None,
**kwargs
):
"""Create rectangle marks."""
# Text marks
def mark_text(
self,
align=None,
angle=None,
baseline=None,
font=None,
fontSize=None,
fontStyle=None,
fontWeight=None,
**kwargs
):
"""Create text marks."""
# Other marks
def mark_tick(
self,
thickness=None,
**kwargs
):
"""Create tick marks."""
def mark_image(
self,
url=None,
aspect=None,
**kwargs
):
"""Create image marks."""
def mark_arc(
self,
innerRadius=None,
outerRadius=None,
**kwargs
):
"""Create arc marks for pie/donut charts."""Methods for creating charts with composite marks that combine multiple primitive marks.
def mark_boxplot(
self,
extent=None,
size=None,
**kwargs
):
"""Create box plot marks showing distribution statistics."""
def mark_errorbar(
self,
extent=None,
thickness=None,
**kwargs
):
"""Create error bar marks showing uncertainty."""
def mark_errorband(
self,
extent=None,
borders=None,
**kwargs
):
"""Create error band marks showing confidence intervals."""Methods for working with chart data sources.
def add_data(self, data):
"""Add or replace chart data source."""
def add_data_transformer(self, transformer):
"""Add data transformation function."""import altair as alt
import pandas as pd
# Create data
data = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y': [2, 5, 3, 8, 7]
})
# Create basic scatter plot
chart = alt.Chart(data).mark_circle(size=100).encode(
x='x:Q',
y='y:Q'
)# Chart with color, size, and tooltip
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])),
tooltip=['x:Q', 'y:Q', 'category:N', 'value:Q']
).properties(
width=400,
height=300,
title='Multi-encoded Scatter Plot'
)# Chart with filtering and aggregation
chart = alt.Chart(data).mark_bar().encode(
x='category:N',
y='mean(value):Q'
).transform_filter(
alt.datum.value > 10
).properties(
title='Filtered Average Values by Category'
)from typing import Union, Dict, Any, Optional, List
# Mark configuration types
MarkConfig = Dict[str, Any]
EncodingDict = Dict[str, Any]
# Chart properties
class ChartProperties:
width: Optional[int]
height: Optional[int]
title: Optional[Union[str, Dict[str, Any]]]
background: Optional[str]
padding: Optional[Union[int, Dict[str, int]]]
# Data types
DataSource = Union[pd.DataFrame, str, Dict[str, Any], List[Dict[str, Any]]]
# Mark types
MarkType = Union[
'point', 'circle', 'square', 'line', 'trail', 'rule',
'area', 'bar', 'rect', 'text', 'tick', 'image', 'arc',
'boxplot', 'errorbar', 'errorband'
]Install with Tessl CLI
npx tessl i tessl/pypi-altair