簡體   English   中英

如何將默認值傳遞給 altair 編碼

[英]How to pass default value to altair encoding

我想根據條件改變shape參數,在某些情況下希望它保持默認值。 然而,以下內容:

import altair as A
shape = A.Undefined
ch = A.Chart({}).encode(shape=shape)

引發TypeError: argument of type 'UndefinedType' is not iterable 這是一個錯誤,還是我可以傳遞適當的“空”值?

Altair 的 API 目前不支持任何標記來表示空編碼。 如果這對您的用例很重要,您可以在https://github.com/altair-viz/altair/issues打開功能請求

最好的解決方法是做這樣的事情:

import altair as alt
shape = None

kwds = {} if shape is None else {'shape': shape}
ch = alt.Chart().encode(**kwds)

如果您正在尋找工具提示和大小的默認值,我使用了下面的示例。

import altair as alt
from vega_datasets import data

source = data.iris()
if size is None:
    size = {}
if tooltip is None:
    tooltip = []
alt.Chart(source).mark_circle().encode(
    alt.X('sepalLength', scale=alt.Scale(zero=False)),
    alt.Y('sepalWidth', scale=alt.Scale(zero=False, padding=1)),
    color='species',
    size=size,
    tooltip=tooltip
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM