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

Altair库AttributeError:无selection_point属性的技术求助

问题原因与解决方案

报错根源

alt.selection_point 是 Altair 5.0 及以上版本 才新增的选择器API,你当前使用的Altair版本低于5.0,所以会抛出AttributeError。

可以通过以下代码确认当前版本:

print(alt.__version__)

解决方案

方案1:升级Altair到最新版本

在Colab中执行升级命令:

!pip install --upgrade altair

升级后注意修正代码中fields参数的错误(需传入字段名字符串列表,而非Series对象),完整代码示例:

import altair as alt
import pandas as pd
data = pd.read_csv("ds_salaries.csv")

graph1 = alt.Chart(data).mark_bar().encode( 
    x='count(job_title)',
    y='job_title'
)

source1_drop = alt.binding_select(options=['EN', 'SE'], name="Experience Level (Source 1)")
source2_drop = alt.binding_select(options=['EN', 'SE'], name="Experience Level (Source 2)")

# 修正fields参数为字符串列表,并绑定选择器
source1_select = alt.selection_point(fields=['experience_level'], bind=source1_drop)
graph1 = graph1.add_params(source1_select).transform_filter(source1_select)

graph1

方案2:使用旧版本兼容的选择器API

如果不想升级版本,用旧版本的alt.selection_single替代selection_point,参数逻辑一致:

import altair as alt
import pandas as pd
data = pd.read_csv("ds_salaries.csv")

graph1 = alt.Chart(data).mark_bar().encode( 
    x='count(job_title)',
    y='job_title'
)

source1_drop = alt.binding_select(options=['EN', 'SE'], name="Experience Level (Source 1)")
source2_drop = alt.binding_select(options=['EN', 'SE'], name="Experience Level (Source 2)")

# 用selection_single替代selection_point
source1_select = alt.selection_single(fields=['experience_level'], bind=source1_drop)
graph1 = graph1.add_params(source1_select).transform_filter(source1_select)

graph1

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 22:52:03