Search code examples
pythonaltair

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?


Solution

  • 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)