簡體   English   中英

在 Altair LayerChart 中指定 plot 標題和構面標題

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

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

在此處輸入圖像描述

在這里,我可以分別控制主要的 plot 標題和構面的標題。

現在假設我要創建相同的圖表,但為每個條添加文本注釋:

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

在此處輸入圖像描述

這一次,刻面上方有species名稱。 在這種情況下,如何控制主要 plot 標題和構面標題?

傳遞給facet方法的關鍵字 arguments 被傳遞給創建的alt.FacetChart圖表。

您想要傳遞給構面編碼的任何關鍵字 arguments 都可以通過alt.Facet()包裝器傳遞,類似於其他編碼。

例如:

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"]})

返回這個

感謝您提出的問題及其滿足我需求的解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM