简体   繁体   中英

How to pass default value to altair encoding

I would like to vary the shape argument based on conditions, and in some cases would like it to remain as the default value. The following, however:

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

raises TypeError: argument of type 'UndefinedType' is not iterable . Is this a bug, or is there an appropriate 'null' value I can pass?

Altair's API does not currently support any sentinel to represent an empty encoding. If this is important to your use-case, you could open a feature request at https://github.com/altair-viz/altair/issues

The best workaround would be to do something like this:

import altair as alt
shape = None

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

If you are looking for defaults for tooltip and size, I used the example below.

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM