Altair label conditional color
Does anyone know how to conditionally color an Altair chart label based on the value of another column? Struggling to find this one on Google
22 Replies
are you using
mark_text()
?
Maybe something like:
I'm not, just a fairly basic chart. I'm just trying to change the color of the axis labels. This is what I'm doing and it's not throwing an error but the conditional isn't working as expected
I'm also doing a conditional sort based on a checkbox that isn't working
I don't know why I can't get any conditionals to work right with this chart 🤦🏼♀️
are you using
mo.ui.altair_chart
, if so, can you try without it? we add some of our own conditions and not sure if they are conflicting
is the axis at least green or red? or its neither color?it's red, so it is responding, just not correctly
if you flip to
alt.datum.Climbed < 0.1
does the color flip?it does not
I also tried removing
mo.ui.altair_chart
and it didn't changeif you mis-spell
Climbed
, does it error?it's like it's not correctly referencing the
Climbed
field but I'm not sure what else to try to reference it
no, it doesn'ti wonder if that field is somehow being dropped. maybe its the fold?
if you return
avg_stacked_bar
, do you see Climbed
in the dataframe?avg_stacked_bar
is the chart itself. I'm not sure how to pull out the dataframe itself after the foldsorry
avg_stacked_bar.data
it's there...
can you try
alt.datum.Climbed == "0.2"
in case its being converted to a stringall red still
I got this one fixed and the issue was unrelated
I also tried simplifying down the chart and adding parenthesis to the logic to make sure it was evaluating in the correct order with no change in behavior
nothing looks wrong to me there. im pretty stumped. if i had to guess - maybe
lableColor
isnt conditionally colorableI am confident that you can, because there are StackOverflow posts about it, and it is doing something, it's just not evaluating the conditional correctly.
Stack Overflow
Changing the color of the chart label axis
My visualization:
I have made the above visualization with altair and would like to know if it is possible to add other colors to the labels like I did with hook, that matches the color of the leg...
I also tried using the same conditional on another property and am having the same issue
I just did a dumb experiment to make sure that the conditional worked in general and it does. it's specifically something with the
Climbed
comparisonI finally got it to work how I wanted it to by iterating through the dataframe to check for the condition, creating a list of labels that satisfy the condition, then making that the conditional. Still no success just directly doing the conditional in the chart
good enough for me though, it works
ah that is a smart workaround! glad you figured it out