简体   繁体   English

如何在 altair 中安装新字体并在 alt.TitleParams 中指定它

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

I am wondering if it's possible to install fonts to use in altair to use in alt.TitleParams.我想知道是否可以安装 fonts 以在 altair 中使用以在 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'
)

在此处输入图像描述

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'
)

在此处输入图像描述

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'
)

在此处输入图像描述

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:一样:

在此处输入图像描述

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.我想知道如何显示正确的字体,不仅是标准的 fonts,还有非标准的字体以及如何安装它们。

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: fontStyle是指字体的样式,如“粗体”、“斜体”等。如果要通过名称指定字体,使用font参数:

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM