在Altair LayerChart中指定图表标题和分面标题

8
使用鸢尾花数据集,我们可以创建一个简单的分面图表:
import altair as alt
from vega_datasets import data
iris = data.iris.url

alt.Chart(iris, title='Main Title').mark_bar().encode(
    x='petalWidth:Q',
    y='count(petalLength):Q',
    color='species:N',
    facet=alt.Facet('species:N', title=None)
)

enter image description here

我可以控制主图标题和分面标题。

现在假设我想创建相同的图表,但要向每个条形图添加文本注释:

base = alt.Chart(iris).encode(
    x='petalWidth:Q',
    y='count(petalLength):Q',
    color='species:N',
    text='count(petalLength):Q'
)

c = base.mark_bar()
t = base.mark_text(dy=-6)

alt.layer(c, t).facet('species:N', title=None).properties(title='Main Title')

enter image description here

这次,图表上方有一个“物种”标题。在这种情况下,我如何控制主图标题和分面标题?
2个回答

9

传递给facet方法的关键字参数会传递到创建的alt.FacetChart图表中。

您想要传递给facet编码的任何关键字参数都可以通过alt.Facet()包装器传递,类似于其他编码方式。

例如:

alt.layer(c, t).facet(
    facet=alt.Facet('species:N', title=None),
    title='Main Title'
)

enter image description here


你介意在 issue 中提到的解决方案更新一下你的回答吗? - C. Braun

0
gm_plot = alt.Chart(gm2018, width=200, height=150).mark_bar(opacity=0.7).encode(
alt.X('life_expectancy', bin=alt.Bin(maxbins=30)),
alt.Y('count()'),
alt.Color('region')).facet( facet=alt.Facet(
'region:N', title="test where the title in the facet arg goes"),
        title={'text': ["can i pass a dict as in normal .properties()?",
                        " and get multilined titles"],
               "subtitle" : ["multilined subtitles too?", "yes"]})

返回这个

感谢您的问题和解决方案。alt.Facet 对我的需求很有帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接