Search code examples
altair

How can I add math symbols to altair plots?


Is there a way to add mathematical symbols like beta, alpha, epsilon, etc. into altair plots?

I'd like to be able to add them really wherever — e.g., axis titles, text marks, ...

Is this possible? I could not find anything in the Altair/Vega documents that spoke to this.


Solution

  • As I found in other discussion (https://github.com/altair-viz/altair/issues/1105)- Only Unicode characters are supported in Vega.

    You can search for the corresponding superscript character at this page: https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts and copy the character of interest in your specification.

    This is how it can look:`

    import altair as alt
    import pandas as pd
    
    source = pd.DataFrame({
        'a': ['A', 'B', 'C', 'D'],
        'b': [28, 55, 43, 91]
    })
    
    alt.Chart(source).mark_bar().encode(
        y=alt.Y('a', title="βγδθ𝛼"),
        x=alt.X('b', title="βγδθ𝛼")
    ).properties(
        title={
            "text":["Here are βγδθ𝛼 in title "], 
            "subtitle": ["Some more in subtitle -φχημα"],
        }
    )
    

    `https://imgur.com/a/BvaGKhZchart sample