Mady
Mady8mo ago

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
Myles Scolnick
Myles Scolnick8mo ago
are you using mark_text()? Maybe something like:
encode(
....
color=alt.condition(
alt.datum.highlight, # Condition based on the 'highlight' column
alt.value('red'), # Color if condition is true
alt.value('black') # Color if condition is false
)
encode(
....
color=alt.condition(
alt.datum.highlight, # Condition based on the 'highlight' column
alt.value('red'), # Color if condition is true
alt.value('black') # Color if condition is false
)
Mady
MadyOP8mo ago
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
No description
Mady
MadyOP8mo ago
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 🤦🏼‍♀️
Myles Scolnick
Myles Scolnick8mo ago
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?
Mady
MadyOP8mo ago
it's red, so it is responding, just not correctly
Myles Scolnick
Myles Scolnick8mo ago
if you flip to alt.datum.Climbed < 0.1 does the color flip?
Mady
MadyOP8mo ago
it does not I also tried removing mo.ui.altair_chart and it didn't change
Myles Scolnick
Myles Scolnick8mo ago
if you mis-spell Climbed , does it error?
Mady
MadyOP8mo ago
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't
Myles Scolnick
Myles Scolnick8mo ago
i 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?
Mady
MadyOP8mo ago
avg_stacked_bar is the chart itself. I'm not sure how to pull out the dataframe itself after the fold
Myles Scolnick
Myles Scolnick8mo ago
sorry avg_stacked_bar.data
Mady
MadyOP8mo ago
it's there...
No description
Myles Scolnick
Myles Scolnick8mo ago
can you try alt.datum.Climbed == "0.2" in case its being converted to a string
Mady
MadyOP8mo ago
all red still I got this one fixed and the issue was unrelated
Mady
MadyOP8mo ago
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
No description
Myles Scolnick
Myles Scolnick8mo ago
nothing looks wrong to me there. im pretty stumped. if i had to guess - maybe lableColor isnt conditionally colorable
Mady
MadyOP8mo ago
I 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...
Mady
MadyOP8mo ago
I also tried using the same conditional on another property and am having the same issue
Mady
MadyOP8mo ago
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 comparison
No description
Mady
MadyOP8mo ago
I 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
Myles Scolnick
Myles Scolnick8mo ago
ah that is a smart workaround! glad you figured it out