Specify plot title and facet title in Altair LayerChart


Using the iris dataset we can create a simple faceted chart:

import altair as alt
from vega_datasets import data
iris = data.iris.url

alt.Chart(iris, title='Main Title').mark_bar().encode(
    facet=alt.Facet('species:N', title=None)

enter image description here

Here I can control both the main plot title and the title of the facets respectively.

Now let’s say I want create the same chart, but add text annotations to each bar:

base = alt.Chart(iris).encode(

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

This time, there is the species title above the facets. How can I control both the main plot title and the facet title in this case?

Asked By: C. Braun



Keyword arguments passed to the facet method are passed through to the alt.FacetChart chart that is created.

Any keyword arguments you want passed to the facet encoding can be passed via an alt.Facet() wrapper, similar to other encodings.

For example:

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

enter image description here

Answered By: jakevdp
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.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"]})

returns this

Thanks for the question and the solution. The alt.Facet worked for my needs

Answered By: phidelity
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.