Claude Code for Altair: Declarative Statistical Visualization — Claude Skills 360 Blog
Blog / AI / Claude Code for Altair: Declarative Statistical Visualization
AI

Claude Code for Altair: Declarative Statistical Visualization

Published: November 17, 2027
Read time: 5 min read
By: Claude Skills 360

Altair is a declarative visualization library based on Vega-Lite. pip install altair. import altair as alt. Basic chart: alt.Chart(df).mark_point().encode(x="col_a:Q", y="col_b:Q", color="category:N"). Shorthand data types: :Q quantitative, :O ordinal, :N nominal, :T temporal. Mark types: mark_point, mark_line, mark_bar, mark_area, mark_circle, mark_rect, mark_boxplot, mark_rule. Encodings: alt.X("col:Q", bin=True), alt.Y("count():Q"), alt.Color("c:N", scale=alt.Scale(scheme="category10")), alt.Size("val:Q"), alt.Tooltip(["a", "b", "c"]). Interactive: selection = alt.selection_interval(), chart.add_params(selection).encode(color=alt.condition(selection, "cat:N", alt.value("lightgray"))). Point selection: sel = alt.selection_point(fields=["category"]). Transforms: .transform_filter(alt.datum.value > 0), .transform_aggregate(mean_val="mean(value)", groupby=["group"]), .transform_bin("x_bin", "x"), .transform_calculate("log_x", "log(datum.x)"), .transform_fold(["a","b","c"], as_=["key","value"]). Facet: chart.facet(column="col:N", row="row:N") or shorthand facet(facet=alt.Facet("col:N", columns=3)). Composition: chart1 | chart2 (hconcat), chart1 & chart2 (vconcat), alt.layer(base, points). Resolve: .resolve_scale(y="independent"). Properties: .properties(width=400, height=300, title="My Chart"). Save: chart.save("chart.html") or chart.save("chart.json"). Show: chart in Jupyter renders automatically. Claude Code generates Altair interactive dashboards, linked selection charts, small-multiples facets, and Vega-Lite JSON specs.

CLAUDE.md for Altair

## Altair Stack
- Version: altair >= 5.0 (uses Vega-Lite v5)
- Chart: alt.Chart(df).mark_TYPE().encode(x=, y=, color=, tooltip=)
- Types: :Q quantitative | :O ordinal | :N nominal | :T temporal
- Marks: mark_point/line/bar/area/circle/rect/boxplot/rule/text
- Interactive: selection_interval/selection_point + add_params() + condition()
- Transforms: transform_filter/aggregate/bin/calculate/fold/window/joinaggregate
- Compose: chart1 | chart2 (H) | chart1 & chart2 (V) | alt.layer(c1, c2)
- Export: chart.save("path.html") | chart.to_dict() for JSON spec

Altair Declarative Visualization Pipeline

# viz/altair_pipeline.py — declarative statistical visualization with Altair
from __future__ import annotations
import numpy as np
import pandas as pd
import altair as alt
from pathlib import Path

# Enable max rows for large datasets (default is 5000)
alt.data_transformers.enable("vegafusion")   # or "default" for small data


# ── 1. Basic chart builders ───────────────────────────────────────────────────

def scatter_chart(
    df:          pd.DataFrame,
    x:           str,
    y:           str,
    color:       str = None,
    size:        str = None,
    tooltip:     list[str] = None,
    title:       str = "",
    width:       int = 400,
    height:      int = 300,
) -> alt.Chart:
    """
    Interactive scatter plot.
    Supports hover tooltip, zoom, and pan by default.
    """
    encoding = {
        "x": alt.X(f"{x}:Q", title=x),
        "y": alt.Y(f"{y}:Q", title=y),
    }
    if color:
        encoding["color"] = alt.Color(f"{color}:N")
    if size:
        encoding["size"] = alt.Size(f"{size}:Q")
    if tooltip:
        encoding["tooltip"] = tooltip

    return (
        alt.Chart(df)
        .mark_circle(opacity=0.7)
        .encode(**encoding)
        .properties(width=width, height=height, title=title)
        .interactive()
    )


def bar_chart(
    df:          pd.DataFrame,
    x:           str,
    y:           str,
    color:       str = None,
    horizontal:  bool = False,
    sort:        str = "-y",   # "-y" descending, "x" ascending
    title:       str = "",
    width:       int = 400,
    height:      int = 300,
) -> alt.Chart:
    """Bar chart with optional color grouping."""
    if horizontal:
        x_enc = alt.X(f"{y}:Q", title=y)
        y_enc = alt.Y(f"{x}:N", sort=sort, title=x)
    else:
        x_enc = alt.X(f"{x}:N", sort=sort, title=x)
        y_enc = alt.Y(f"{y}:Q", title=y)

    encoding = {"x": x_enc, "y": y_enc}
    if color:
        encoding["color"] = alt.Color(f"{color}:N")

    return (
        alt.Chart(df)
        .mark_bar()
        .encode(**encoding)
        .properties(width=width, height=height, title=title)
    )


def line_chart(
    df:          pd.DataFrame,
    x:           str,
    y:           str,
    color:       str = None,
    x_type:      str = "T",   # "T" temporal, "Q" quantitative, "O" ordinal
    point:       bool = True,
    title:       str = "",
    width:       int = 500,
    height:      int = 280,
) -> alt.Chart:
    """Line chart with optional point markers."""
    encoding = {
        "x": alt.X(f"{x}:{x_type}", title=x),
        "y": alt.Y(f"{y}:Q", title=y),
    }
    if color:
        encoding["color"] = alt.Color(f"{color}:N")

    mark_kwargs = {"point": {"filled": True, "size": 40}} if point else {}
    return (
        alt.Chart(df)
        .mark_line(**mark_kwargs)
        .encode(**encoding)
        .properties(width=width, height=height, title=title)
        .interactive()
    )


def histogram(
    df:         pd.DataFrame,
    col:        str,
    n_bins:     int = 30,
    color:      str = None,
    title:      str = "",
    width:      int = 400,
    height:     int = 260,
) -> alt.Chart:
    """Histogram with Altair binning."""
    encoding = {
        "x": alt.X(f"{col}:Q", bin=alt.Bin(maxbins=n_bins), title=col),
        "y": alt.Y("count():Q", title="Count"),
    }
    if color:
        encoding["color"] = alt.Color(f"{color}:N")

    return (
        alt.Chart(df)
        .mark_bar(opacity=0.7)
        .encode(**encoding)
        .properties(width=width, height=height, title=title or f"Distribution of {col}")
    )


# ── 2. Interactive selections ─────────────────────────────────────────────────

def linked_scatter_with_hist(
    df:    pd.DataFrame,
    x:     str,
    y:     str,
    color: str = None,
) -> alt.VConcatChart:
    """
    Linked scatter + histogram.
    Brush in the scatter highlights bars in the histogram.
    """
    brush = alt.selection_interval()
    highlight = alt.condition(brush, alt.value(0.8), alt.value(0.1))

    scatter = (
        alt.Chart(df)
        .mark_circle()
        .encode(
            x=f"{x}:Q", y=f"{y}:Q",
            color=alt.Color(f"{color}:N") if color else alt.value("steelblue"),
            opacity=highlight,
            tooltip=[x, y] + ([color] if color else []),
        )
        .add_params(brush)
        .properties(width=400, height=280, title="Select a region to highlight")
    )

    hist = (
        alt.Chart(df)
        .mark_bar()
        .encode(
            x=alt.X(f"{x}:Q", bin=alt.Bin(maxbins=30)),
            y="count():Q",
            color=alt.condition(brush, alt.value("steelblue"), alt.value("lightgray")),
        )
        .transform_filter(brush)
        .properties(width=400, height=120, title=f"Filtered distribution of {x}")
    )

    return scatter & hist


def multi_series_selector(
    df:          pd.DataFrame,
    x:           str,
    y:           str,
    series:      str,
) -> alt.LayerChart:
    """
    Multi-series line chart with click-to-highlight a series.
    """
    sel = alt.selection_point(fields=[series], bind="legend")

    base = (
        alt.Chart(df).encode(
            x=alt.X(f"{x}:T", title=x),
            y=alt.Y(f"{y}:Q", title=y),
            color=alt.Color(f"{series}:N"),
            opacity=alt.condition(sel, alt.value(1.0), alt.value(0.1)),
            tooltip=[x, y, series],
        )
        .add_params(sel)
        .properties(width=540, height=320)
    )

    return base.mark_line() + base.mark_point()


# ── 3. Data transforms ────────────────────────────────────────────────────────

def grouped_bar_from_long(
    df:      pd.DataFrame,
    x:       str,
    y:       str,
    color:   str,
    title:   str = "",
) -> alt.Chart:
    """Grouped bar chart from long-format DataFrame."""
    return (
        alt.Chart(df)
        .mark_bar()
        .encode(
            x=alt.X(f"{x}:N", title=x),
            y=alt.Y(f"{y}:Q", title=y),
            color=alt.Color(f"{color}:N"),
            xOffset=alt.XOffset(f"{color}:N"),   # Side-by-side grouping
            tooltip=[x, y, color],
        )
        .properties(title=title)
    )


def aggregate_bar(
    df:       pd.DataFrame,
    group:    str,
    value:    str,
    agg_fn:   str = "mean",  # "mean" | "sum" | "count" | "median"
    sort:     str = "-y",
    title:    str = "",
) -> alt.Chart:
    """Bar chart with in-Vega aggregation (no pre-aggregation needed)."""
    return (
        alt.Chart(df)
        .mark_bar()
        .encode(
            x=alt.X(f"{group}:N", sort=sort),
            y=alt.Y(f"{agg_fn}({value}):Q", title=f"{agg_fn}({value})"),
            tooltip=[group, alt.Tooltip(f"{agg_fn}({value}):Q", format=".2f")],
        )
        .properties(title=title or f"{agg_fn}({value}) by {group}")
    )


def binned_heatmap(
    df:      pd.DataFrame,
    x:       str,
    y:       str,
    n_bins:  int = 20,
    title:   str = "",
) -> alt.Chart:
    """2D binned heatmap (density) — good for large scatter datasets."""
    return (
        alt.Chart(df)
        .mark_rect()
        .encode(
            x=alt.X(f"{x}:Q", bin=alt.Bin(maxbins=n_bins)),
            y=alt.Y(f"{y}:Q", bin=alt.Bin(maxbins=n_bins)),
            color=alt.Color("count():Q", scale=alt.Scale(scheme="blues")),
            tooltip=[
                alt.Tooltip(f"{x}:Q", bin=True),
                alt.Tooltip(f"{y}:Q", bin=True),
                "count():Q",
            ],
        )
        .properties(title=title or f"2D Density: {x} vs {y}")
    )


# ── 4. Small multiples (facets) ───────────────────────────────────────────────

def faceted_bars(
    df:         pd.DataFrame,
    x:          str,
    y:          str,
    facet_col:  str,
    columns:    int = 3,
    title:      str = "",
) -> alt.FacetChart:
    """Small multiples bar chart — one panel per facet value."""
    base = (
        alt.Chart(df)
        .mark_bar()
        .encode(
            x=alt.X(f"{x}:N"),
            y=alt.Y(f"{y}:Q"),
            color=alt.Color(f"{x}:N"),
            tooltip=[x, y],
        )
        .properties(width=180, height=140)
    )
    return base.facet(
        facet=alt.Facet(f"{facet_col}:N", columns=columns),
        title=title,
    )


def faceted_lines(
    df:         pd.DataFrame,
    x:          str,
    y:          str,
    facet_col:  str,
    color:      str = None,
    columns:    int = 3,
    independent_y: bool = True,
) -> alt.FacetChart:
    """Faceted line charts, optionally with independent y-axes."""
    base = (
        alt.Chart(df)
        .mark_line()
        .encode(
            x=alt.X(f"{x}:Q"),
            y=alt.Y(f"{y}:Q"),
            color=alt.Color(f"{color}:N") if color else alt.value("steelblue"),
        )
        .properties(width=180, height=140)
    )
    fc = base.facet(
        facet=alt.Facet(f"{facet_col}:N", columns=columns),
    )
    if independent_y:
        fc = fc.resolve_scale(y="independent")
    return fc


# ── 5. Composition ────────────────────────────────────────────────────────────

def scatter_with_marginals(
    df:    pd.DataFrame,
    x:     str,
    y:     str,
    color: str = None,
) -> alt.VConcatChart:
    """
    Scatter plot with marginal histogram strips (top + right).
    Classic bivariate distribution view.
    """
    points = (
        alt.Chart(df)
        .mark_circle(opacity=0.6)
        .encode(
            x=alt.X(f"{x}:Q"),
            y=alt.Y(f"{y}:Q"),
            color=alt.Color(f"{color}:N") if color else alt.value("steelblue"),
            tooltip=[x, y] + ([color] if color else []),
        )
        .properties(width=360, height=300)
    )

    top_hist = (
        alt.Chart(df)
        .mark_bar(opacity=0.6)
        .encode(
            x=alt.X(f"{x}:Q", bin=alt.Bin(maxbins=25), axis=None),
            y=alt.Y("count():Q", title=""),
        )
        .properties(width=360, height=60)
    )

    return top_hist & points


# ── 6. Export ─────────────────────────────────────────────────────────────────

def save_chart(chart, path: str) -> str:
    """Save chart as HTML (interactive) or JSON (Vega-Lite spec)."""
    Path(path).parent.mkdir(parents=True, exist_ok=True)
    chart.save(path)
    print(f"Chart saved: {path}")
    return path


def chart_to_spec(chart) -> dict:
    """Return the Vega-Lite JSON specification as a Python dict."""
    return chart.to_dict()


# ── Demo ──────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    import tempfile, os
    print("Altair Declarative Visualization Demo")
    print("=" * 50)

    np.random.seed(42)
    n = 300
    df = pd.DataFrame({
        "x":       np.random.randn(n),
        "y":       np.random.randn(n) * 1.5 + 1,
        "size":    np.random.exponential(20, n),
        "group":   np.random.choice(["A", "B", "C"], n),
        "month":   pd.date_range("2024-01-01", periods=n, freq="D")[:n],
        "value":   np.cumsum(np.random.randn(n)) + 50,
    })

    with tempfile.TemporaryDirectory() as tmpdir:
        # Scatter
        sc = scatter_chart(df, "x", "y", color="group", tooltip=["x","y","group"])
        save_chart(sc, f"{tmpdir}/scatter.html")

        # Bar
        bc = aggregate_bar(df, "group", "value", agg_fn="mean")
        save_chart(bc, f"{tmpdir}/bar.html")

        # Linked
        linked = linked_scatter_with_hist(df, "x", "y", color="group")
        save_chart(linked, f"{tmpdir}/linked.html")

        # Binned heatmap
        hm = binned_heatmap(df, "x", "y")
        save_chart(hm, f"{tmpdir}/heatmap.html")

        # Faceted
        facet_df = pd.DataFrame({
            "product": np.repeat(["A","B","C","D"], 50),
            "region":  np.tile(np.random.choice(["N","S","E","W"], 50), 4),
            "sales":   np.random.exponential(100, 200),
        })
        fc = faceted_bars(facet_df, "region", "sales", "product", columns=2)
        save_chart(fc, f"{tmpdir}/facet.html")

        print(f"\nAll Altair charts saved to {tmpdir}")
        print("Open .html files in a browser to see interactive charts")

    # Print spec excerpt
    small_chart = bar_chart(
        pd.DataFrame({"cat": list("ABCDE"), "val": [10, 25, 15, 30, 20]}),
        "cat", "val"
    )
    spec = chart_to_spec(small_chart)
    print(f"\nVega-Lite spec keys: {list(spec.keys())}")

For the Matplotlib/Seaborn alternative for static publication plots — Matplotlib/Seaborn produce print-ready PDFs while Altair’s Vega-Lite declarative spec generates interactive SVG-based HTML charts with automatic tooltips, zoom, and selection linking across panels, and the selection_interval brush that filters linked charts requires zero JavaScript — it’s expressed as a Python data binding, making Altair the fastest path from a pandas DataFrame to a cross-filtered interactive dashboard. For the Plotly Express alternative when drop-in interactive charts are needed — Plotly Express is imperative (one call per chart type) while Altair’s grammar composes arbitrary mark types and encodings, transform_fold unpivots wide DataFrames inside the spec, and facet(columns=3) auto-wraps small multiples without computing subplot geometry, making Altair more powerful for statistical reporting where the chart structure should mirror the data model. The Claude Skills 360 bundle includes Altair skill sets covering scatter, bar, line, histogram, and binned heatmap, interactive brushing with selection_interval and selection_point, linked charts, multi-panel facets, grouped bars with xOffset, data transforms, and chart composition with hconcat, vconcat, and layer. Start with the free tier to try declarative visualization code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free