运行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
相关产品推荐
相关产品推荐

