vega / altair

Declarative statistical visualization library for Python
https://altair-viz.github.io/
BSD 3-Clause "New" or "Revised" License
9.4k stars 795 forks source link

Save to PNG with Top-Level configuration #3617

Open Amarounek opened 1 month ago

Amarounek commented 1 month ago

What happened?

Hey,

I want to save my figure to PNG with a different font but it doesn't seem to work properly.

_weekly_fig_2003

My mini snippet:

combined_chart = alt.layer(bar_year_prec + rule).configure(font='Roboto') 
combined_chart.save(test.png,  ppi = 300)

What would you like to happen instead?

Actually, it is working fine if I don't configure the font overall...

Which version of Altair are you using?

5.4.1

jonmmease commented 1 month ago

Hi @Amarounek,

Are you saying that the font specification works if you do it at a lower level, instead of with a top-level configure? Or that setting to Roboto never works?

chart.save uses vl-convert. There is some info on font configuration here https://github.com/vega/vl-convert?tab=readme-ov-file#system-font-requirements.

In particular, if you can locate the directory that contains the Roboto font on your system you can register the fonts with:

import vl_convert as vlc
vlc.register_font_directory('/path/to/roboto')

That said, usually when a font can't be located the chart falls back to using a generic system font, so it's surprising that no text is displayed.

Amarounek commented 1 month ago

@jonmmease Thanks for your time.

I've tried the lower-level (.configure_axis(labelFont='Arial',) and yes, the targeted aspect also disappeared (Labels). Ok I will tried the vlc register later...