You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Altair自定义主题中使用嵌套参数?(facet标题设置)

解决Altair自定义主题修改分面标题字体不生效的问题

你的代码问题出在主题配置的层级上,针对分面列标题的字体,需要配置column.header.labelFont(仅列分面生效)或全局header.labelFont(行/列分面通用),而非facet.header.labelFont。

修正后的代码方案

方案1:仅修改列分面标题字体

import altair as alt
from vega_datasets import data
iris = data.iris.url

def custom_theme():
    return {
        'config': {
            'column': {
                'header': {
                    'labelFont': 'Lato'
                }
            }
        }
    }

alt.themes.register('custom_theme', custom_theme)
alt.themes.enable('custom_theme')

alt.Chart(iris).mark_point().encode(
    x='petalLength:Q',
    y='petalWidth:Q',
    color='species:N'
).properties(
    width=180,
    height=180
).facet(
    column='species:N'
)

方案2:全局修改所有分面标题字体(行/列都生效)

import altair as alt
from vega_datasets import data
iris = data.iris.url

def custom_theme():
    return {
        'config': {
            'header': {
                'labelFont': 'Lato'
            }
        }
    }

alt.themes.register('custom_theme', custom_theme)
alt.themes.enable('custom_theme')

alt.Chart(iris).mark_point().encode(
    x='petalLength:Q',
    y='petalWidth:Q',
    color='species:N'
).properties(
    width=180,
    height=180
).facet(
    column='species:N'
)

原因说明

在Altair依赖的Vega-Lite规范中,facet配置项负责分面的整体布局逻辑,而具体的行/列分面标题样式属于row或column的子配置。如果想统一所有分面标题的样式,直接配置全局header即可覆盖所有行、列分面的标题字体。

内容的提问来源于stack exchange,提问作者rbb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 11:11:09