Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install a new font in altair and specifying it in alt.TitleParams

Tags:

python

altair

I am wondering if it's possible to install fonts to use in altair to use in alt.TitleParams. For this example, without font specified, I get a default font and size.

import altair as alt
import pandas as pd

source = pd.DataFrame({
    'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
    'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})

alt.Chart(source, title = alt.TitleParams(text = 'Example Chart')).mark_bar().encode(
    x='a',
    y='b'
)

enter image description here

Changing the font size I get the bigger letters:

alt.Chart(source, title = alt.TitleParams(text = 'Example Chart', fontSize=24)).mark_bar().encode(
    x='a',
    y='b'
)

enter image description here

But, when I add a font style, the size doesn't work anymore:

alt.Chart(source, title = alt.TitleParams(text = 'Example Chart'
                                          , fontSize=24
                                          , fontStyle = 'Arial')).mark_bar().encode(
    x='a',
    y='b'
)

enter image description here

And the text looks always the same regardless of what font is specified:

alt.Chart(source, title = alt.TitleParams(text = 'Example Chart'
                                          , fontSize=24
                                          , fontStyle = 'Calibri')).mark_bar().encode(
    x='a',
    y='b'
)

Same thing:

enter image description here

I would like to know how I can display the correct font, not only with standard fonts, but with non-standard ones and how to install them.

like image 391
flbyrne Avatar asked Nov 01 '25 13:11

flbyrne


1 Answers

fontStyle refers to the style of the font, such as "bold", "italic", etc. If you want to specify a font by name, use the font parameter:

alt.Chart(
    source,
    title=alt.Title(  # Since altair 5 you can use just Title instead of TitleParams
        text='Example Chart',
        fontSize=24,
        fontStyle='italic',
        font='Times'
    )
).mark_bar().encode(
    x='a',
    y='b'
)

enter image description here

like image 192
joelostblom Avatar answered Nov 03 '25 04:11

joelostblom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!