我想要做到这一点:
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"mark": {"type": "bar", "tooltip": true},
"encoding": {
"x": {"field": "Creative Type", "type": "nominal", "aggregate": null},
"y": {"field": "Creative Type", "type": "nominal", "aggregate": "count"}
我将值从Python传递给了vegalite。因为Python没有null关键字,所以我不知道如何在x轴中将aggregate的值设置为null。任何帮助都是非常感谢的。谢谢!
发布于 2021-02-16 10:19:46
在Python中,null关键字的等价物将是None。请检查一下是否有效。
发布于 2021-02-16 14:10:42
通常,在Python/Altair中传递None
将导致在JSON/vega中使用null
。例如,当隐藏一个图例(织女星中的"legend": null
)时,你可以在牛郎星中使用legend=None
,就像Adjusting the Legend中提到的那样。
然而,在聚合的情况下,这将不起作用:根据Vega-Lite模式,null
不是aggregate
的有效参数,因此在Altair中尝试此操作将导致SchemaValidationError
。
如果由于某些原因您确实想要生成这个无效的规范,可以通过将validate=False
传递给alt.Chart.to_json
来实现
import altair as alt
chart = alt.Chart().mark_bar(tooltip=True).encode(
x=alt.X('Creative Type:N', aggregate=None),
y=alt.Y('Creative Type:N', aggregate='count')
)
print(chart.to_json(validate=False))
# {
# "$schema": "https://vega.github.io/schema/vega-lite/v4.8.1.json",
# "config": {
# "view": {
# "continuousHeight": 300,
# "continuousWidth": 400
# }
# },
# "encoding": {
# "x": {
# "aggregate": null,
# "field": "Creative Type",
# "type": "nominal"
# },
# "y": {
# "aggregate": "count",
# "field": "Creative Type",
# "type": "nominal"
# }
# },
# "mark": {
# "tooltip": true,
# "type": "bar"
# }
# }
https://stackoverflow.com/questions/66221425
复制相似问题