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

运行Altair代码时遭遇TypeError: 'UndefinedType' object is not callable错误求助

解决Altair中TypeError: 'UndefinedType' object is not callable错误

这个错误是因为你使用的Altair 5.x及以上版本移除了.axis()、.title()这类链式调用的API写法,旧版的链式配置方式不再被支持,调用不存在的方法就会触发该报错。

修正后的代码

import altair as alt
from vega_datasets import data
cars = data.cars()

alt.Chart(cars).mark_point().encode(
    x=alt.X('Horsepower', axis=alt.Axis(tickMinStep=50)),
    y=alt.Y('Miles_per_Gallon', title='Miles per Gallon'),
    color='Origin',
    shape='Origin'
)

修改说明

  • 将alt.X('Horsepower').axis(tickMinStep=50)调整为alt.X('Horsepower', axis=alt.Axis(tickMinStep=50)),直接在alt.X构造函数中通过axis参数传入轴配置对象
  • y轴的标题配置也统一放到alt.Y的参数中,保持API风格一致

兼容旧写法的替代方案

如果需要继续使用链式调用的写法,可以降级到Altair 4.x版本,执行以下命令:

pip install altair==4.2.2

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 18:32:21