altair.theme.register#

altair.theme.register(name, *, enable)#

Decorator for registering a theme function.

Parameters:
name

Unique name assigned in registry.

enable

Auto-enable the wrapped theme.

Examples

Register and enable a theme:

import altair as alt
from altair import theme

@theme.register("param_font_size", enable=True)
def custom_theme() -> theme.ThemeConfig:
    sizes = 12, 14, 16, 18, 20
    return {
        "autosize": {"contains": "content", "resize": True},
        "background": "#F3F2F1",
        "config": {
            "axisX": {"labelFontSize": sizes[1], "titleFontSize": sizes[1]},
            "axisY": {"labelFontSize": sizes[1], "titleFontSize": sizes[1]},
            "font": "'Lato', 'Segoe UI', Tahoma, Verdana, sans-serif",
            "headerColumn": {"labelFontSize": sizes[1]},
            "headerFacet": {"labelFontSize": sizes[1]},
            "headerRow": {"labelFontSize": sizes[1]},
            "legend": {"labelFontSize": sizes[0], "titleFontSize": sizes[1]},
            "text": {"fontSize": sizes[0]},
            "title": {"fontSize": sizes[-1]},
        },
        "height": {"step": 28},
        "width": 350,
    }

We can then see the name parameter displayed when checking:

theme.active
"param_font_size"

Until another theme has been enabled, all charts will use defaults set in custom_theme():

from vega_datasets import data

source = data.stocks()
lines = (
    alt.Chart(source, title=alt.Title("Stocks"))
    .mark_line()
    .encode(x="date:T", y="price:Q", color="symbol:N")
)
lines.interactive(bind_y=False)