Help with how to use a custom font via a ttf file #3880
chrisHassan
started this conversation in
General
Replies: 2 comments
-
|
This works for me in VSCode (had to do a restart after installing the font): import altair as alt
# from altair.datasets import data # altair development version
from vega_datasets import data
# define, register and enable theme
@alt.theme.register("my_custom_font", enable=True)
def my_custom_font() -> alt.theme.ThemeConfig:
font = "VT323"
return {
"config" : {
"title": {'font': font},
"axis": {
"labelFont": font,
"titleFont": font
},
"header": {
"labelFont": font,
"titleFont": font
},
"legend": {
"labelFont": font,
"titleFont": font
}
}
}
# draw the chart
cars = data.cars.url
point = alt.Chart(cars, height=100, width=100, title='MY TITLE').mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
)
repeated_sentence = "A B C D E F G H I J K L M N O P"
text = alt.Chart().mark_text(align='left', baseline='bottom', size=10, font='VT323').encode(
text=alt.value(repeated_sentence),
x=alt.datum(100),
y=alt.datum(20),
)
point + text
See also: https://altair-viz.github.io/user_guide/customization.html#defining-a-custom-theme |
Beta Was this translation helpful? Give feedback.
0 replies
-
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm trying to use the custom Google Fonts VT323 and Share Tech Mono on my Mac.
I have confirmed that:
.ttffiles are installed in~/Library/Fonts/.matplotlib.font_manager.findSystemFonts.However, Altair doesn't seem to recognise them when I specify the font name (I suspect it falls back to a default font).
How can I correctly tell Altair to recognise and use these installed custom fonts?
Minimal Reproducible Example
Below is the code I'm using to test the fonts. 'Courier New' works, but 'VT323' and 'Share Tech Mono' do not appear to:
I'm going through this new r book
https://nrennie.rbind.io/art-of-viz/programming-languages.htmlthat uses ggplot and replicating the charts there using Altair. It's helpful to be able to use custom fonts.Beta Was this translation helpful? Give feedback.
All reactions