如何在 PaaS 上的 Plotly (Python) 中指定本地 TTF 文件作为布局字体?

问题描述 投票:0回答:1

我有一个 TrueType 字体文件,我想将其用作使用 Python 中的 Plotly 创建的图表中的布局字体。但是,我无法在系统中手动设置字体,因为图表是在 PaaS(铁路)上生成的。

我尝试过使用

fig.update_layout(font_family="assets/Font-Regular.ttf")
,但是不起作用,只是将字体更改为标准字体。我还尝试使用链接安装字体(就像在这个answer中),但这也不起作用。


这里有一个代码片段供参考:

import plotly.express as px

df = pd.DataFrame({"date": date, "data": data})

fig = px.area(df, x='date', y="data")
fig.update_layout(font_family="assets/Font-Regular.ttf") # This line works incorrect

fig.write_image(f"images/{file_name}.png")

如何指定要在图表中使用的本地字体文件,或者是否有其他方法使用代码设置字体?

python fonts plotly paas
1个回答
0
投票

您可以尝试提供 .ttf 文件的绝对路径,如下所示:

fig.update_layout(font_family=f"{os.path.abspath(os.getcwd())}/assets/Font-Regular.ttf")

© www.soinside.com 2019 - 2024. All rights reserved.