Search code examples
pythonaltair

Type error when using alt.condition for mark point angle


I am trying to plot arrows pointing left or right and being green or red, depending on the condition. It works for the color, but not for the angle of the triangle (mark point) which I'm using for the head of the arrow. Here is the data and code: enter image description here

color=alt.condition("datum.Current >= datum.Previous",alt.value("green"),alt.value("red"))
angle=alt.condition("datum.Current >= datum.Previous",alt.value(210),alt.value(30))
alt.Chart(df_chart).mark_point(size=200,shape='triangle'
                    ,angle=angle).encode(alt.X('Current'),alt.Y('Group'),color=color)

I'm getting this error: enter image description here

This is what I get if I change the angle to a number, it works without error, except that I don't get the red arrow pointing to the left:enter image description here


Solution

  • You can pass alt.condition to the angle encoding rather than the angle mark property:

    alt.Chart(df).mark_point(size=400, shape='triangle').encode(
        alt.X('Current'),
        alt.Y('Group'),
        angle=alt.condition("datum.Current >= datum.Previous", alt.value(210), alt.value(30)),
        color='Group:N'
    )
    

    enter image description here