1

How shall I make some of the x-labels bold with alt.condition? For example with the following code, I try to make the x-label 'C' bold, which is corresponding to the max value 9 on the y-axis.

df = pd.DataFrame({'Name': ['A', 'B', 'C', 'D'], 'Value': [1, 5, 9, 2]})

alt.Chart(df).mark_line().encode(
    x = alt.X(
        'Name:N',
        axis = alt.Axis(
            labelFontWeight = alt.condition(alt.datum.Value == df.Value.max(), alt.value(800), alt.value(300))
        )
    ),
    y = 'Value:Q'
)

It's weird that it always goes to the if-false value, not matter how I change the predict.

Make x-label 'C' bold

sean7x
  • 13
  • 4
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Apr 04 '22 at 14:34
  • Please post a sample of your data as text and show the code you use to read in the data (or use sample data from the altair gallery). This will make it easier for others to help you, more details in [ask] and [mre]. – joelostblom Apr 04 '22 at 14:59
  • If you want to set the conditions directly, you can do it with the following. `labelFontWeight = alt.condition('datum.value == "C"' , alt.value(800), alt.value(300))` – r-beginners Apr 05 '22 at 05:23

1 Answers1

0

I am not sure if you can do that directly in Altair/Vega-Lite, but you could compute the Name for the max Value and compare similar to what was suggested in the comments but slightly more automated using f-strings:

import pandas as pd
import altair as alt


df = pd.DataFrame({'Name': ['A', 'B', 'C', 'D'], 'Value': [1, 5, 9, 2]})
max_name = df['Name'][df['Value'].argmax()]

alt.Chart(df).mark_line().encode(
    x=alt.X(
        'Name:N',
        axis = alt.Axis(
            # datum.label also works
            labelFontWeight=alt.condition(f"datum.value == '{max_name}'", alt.value(800), alt.value(300))
        )
    ),
    y='Value:Q'
)

enter image description here

joelostblom
  • 43,590
  • 17
  • 150
  • 159
  • Thx for helping out. I also realize alt.datum.value only refers to the field 'Name' when it is used in alt.axisX(). Your advice work out perfectly. I just need to get to know about what a f-string is. – sean7x Apr 05 '22 at 06:35
  • You're welcome @sean7x ! f-strings are essentially a convenient way to include variables inside a string that was introduced in Python 3.6. You can see how they compare to previous ways of doing this here https://stackoverflow.com/a/39082102/2166823 – joelostblom Apr 05 '22 at 13:50
  • 1
    Got it now. f-string is like a simple version of string format. Great to know. Appreciated! – sean7x Apr 07 '22 at 00:03
  • 1
    Here's an example: https://cast42.github.io/blog/cast42/jupyter/altair/2022/04/18/Economist-style.html – cast42 Apr 18 '22 at 20:01
  • 1
    Thx @cast42 for the example. It’s a bit different though. In the example it is a fixed value ‘Jeremy Corbyn’ to be bold. What I wanted is the one responding to the max value. The first answer works out perfectly. – sean7x Apr 26 '22 at 05:48