python 当我在vega altair上分层和添加下拉菜单时,为什么会出现SchemaValidationError?

beq87vna  于 2023-05-16  发布在  Python
关注(0)|答案(1)|浏览(118)

我正在尝试用图表中的一些文字来制作一个条形图。在图表中,我可以过滤出具体的年份。出于某种原因,我得到了一个错误,我不知道为什么。下面是我的代码:

source1_drop = alt.binding_select(options=['EN', 'SE','MI','EX'], name="Experience Level (Source 1)")
source1_select = alt.selection_point(fields=['experience_level'], bind=source1_drop)


base1 = alt.Chart(data).encode(
    alt.Y('job_title:N').axis(None)
)
bar1 = base1.mark_bar().encode(
    x='count()'
)
text1 = base1.mark_text(align='left', dx=-145).encode(
    text="job_title:N"
)

filter_exp1 = base1.add_params(source1_select).transform_filter(source1_select).properties(title="Exp Level 1")

上面的代码渲染了这个图像:

输出filter_exp1时出现以下错误:

---------------------------------------------------------------------------
SchemaValidationError                     Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/altair/vegalite/v5/api.py in to_dict(self, *args, **kwargs)
   2511             copy.data = core.InlineData(values=[{}])
   2512             return super(Chart, copy).to_dict(*args, **kwargs)
-> 2513         return super().to_dict(*args, **kwargs)
   2514 
   2515     def add_params(self, *params) -> Self:

1 frames
/usr/local/lib/python3.10/dist-packages/altair/utils/schemapi.py in to_dict(self, validate, ignore, context)
    812                 # show the less helpful ValidationError instead of
    813                 # the more user friendly SchemaValidationError
--> 814                 raise SchemaValidationError(self, err) from None
    815         return result
    816 

SchemaValidationError: '{'data': {'name': 'data-2abd5e3a76b2610869c698a6f5adde06'}, 'encoding': {'y': {'axis': None, 'field': 'job_title', 'type': 'nominal'}}, 'params': [{'name': 'param_1', 'select': {'type': 'point', 'fields': ['experience_level']}, 'bind': {'input': 'select', 'options': ['EN', 'SE', 'MI', 'EX'], 'name': 'Experience Level (Source 1)'}}], 'title': 'Exp Level 1', 'transform': [{'filter': {'param': 'param_1'}}]}' is an invalid value.

'mark' is a required property
alt.Chart(...)

编辑:数据如下:

btqmn9zl

btqmn9zl1#

您看到'mark' is a required property是因为您试图显示不包含标记的图表(base1)。在尝试显示该图表之前添加标记,或者改为显示bar1text1

相关问题