Specify plot title and facet title in Altair LayerChart
Question:
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(
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.
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. How can I control both the main plot title and the facet title in this case?
Answers:
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'
)
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"]})
Thanks for the question and the solution. The alt.Facet worked for my needs
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(
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.
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. How can I control both the main plot title and the facet title in this case?
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'
)
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"]})
Thanks for the question and the solution. The alt.Facet worked for my needs