How do I set a shared title for a Multifaceted Altair Graph?

Viewed 641

I would like to have one main title for my multifaceted (2x2) Altair graph. Currently, each subplot has the title above it, which is very messy.

Here is the code I am using:

    alt.data_transformers.disable_max_rows()
selection = alt.selection_multi(fields=['component_name'], bind='legend')

alt.Chart(top_n)\
     .mark_line(
        strokeWidth = 1.2
    ).encode(
        x = alt.X('click_date_local', title = 'Click Date', axis=alt.Axis(domain=False, format='%b%y', tickSize=0)),
        y = alt.Y('Num_Component_Visits', title = 'Component Clicks/Day'),
        color = alt.Color('component_name', scale=alt.Scale(scheme='tableau20'), legend=alt.Legend(title="Component Name")),
        opacity = alt.condition(selection, alt.value(1), alt.value(0.2)),
        tooltip = ['component_name']
    ).properties(
        width=300,
        height=200, 
        title = 'Clicks per Day for Top 15 Components by Covid Period'
    ).facet(
        column = alt.Column('covid_period', sort = ['Pre', 'Post'], title = 'Covid Period'), 
        row = alt.Row('efficient_flag', title = 'Efficient Flag')
    ).resolve_scale(
        x='independent', 
        y = 'independent'
    ).configure_axis(
        grid=False
    ).add_selection(
        selection
    )

Thanks!

1 Answers

To set a title for the full chart, you can adjust the title property of the facet chart. This is a bit confusing, because title is used in a number of places in chart specifications. Here is an example that will hopefully make the meaning of each more clear:

import altair as alt
from vega_datasets import data

alt.Chart(data.iris()).mark_point().encode(
    x=alt.X('petalLength:Q', title="X Title"),
    y=alt.Y('petalWidth:Q', title="Y Title"),
    color=alt.Color('species:N', title="Color Title")
).properties(
    width=180,
    height=180,
    title='Chart Title'
).facet(
    column=alt.Column('species:N', title='Column Title')
).properties(
    title='Facet Title'
)

enter image description here

Related