简体   繁体   English

在 Altair LayerChart 中指定 plot 标题和构面标题

[英]Specify plot title and facet title in Altair LayerChart

Using the iris dataset we can create a simple faceted chart:使用 iris 数据集,我们可以创建一个简单的多面图:

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

在此处输入图像描述

Here I can control both the main plot title and the title of the facets respectively.在这里,我可以分别控制主要的 plot 标题和构面的标题。

Now let's say I want create the same chart, but add text annotations to each bar:现在假设我要创建相同的图表,但为每个条添加文本注释:

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

在此处输入图像描述

This time, there is the species title above the facets.这一次,刻面上方有species名称。 How can I control both the main plot title and the facet title in this case?在这种情况下,如何控制主要 plot 标题和构面标题?

Keyword arguments passed to the facet method are passed through to the alt.FacetChart chart that is created.传递给facet方法的关键字 arguments 被传递给创建的alt.FacetChart图表。

Any keyword arguments you want passed to the facet encoding can be passed via an alt.Facet() wrapper, similar to other encodings.您想要传递给构面编码的任何关键字 arguments 都可以通过alt.Facet()包装器传递,类似于其他编码。

For example:例如:

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

在此处输入图像描述

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", colums=1),
        title={'text': ["can i pass a dict as in normal .properties()?",
                        " and get multilined titles"],
               "subtitle" : ["multilined subtitles too?", "yes"]})

returns this返回这个

Thanks for the question and the solution it worked for my needs感谢您提出的问题及其满足我需求的解决方案

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

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