Change facet title position in Altair?

Viewed 2367

How can I move the facet titles (in this case, year) to be above each plot? The default seems to be on the side of the chart. Can this be easily changed?

import altair as alt
from vega_datasets import data

df = data.seattle_weather()

alt.Chart(df).mark_rect().encode(
    alt.Y('month(date):O', title='day'),
    alt.X('date(date):O', title='month'),
    color='temp_max:Q'
).facet(
    row='year(date):N',
)

problematic plot

2 Answers

A general solution for this is to use the labelOrient option of the header parameter:

df = df[df.date.dt.year < 2014]  # make a smaller chart

alt.Chart(df).mark_rect().encode(
    alt.Y('month(date):O', title='day'),
    alt.X('date(date):O', title='month'),
    color='temp_max:Q'
).facet(
    row=alt.Row('year(date):N', header=alt.Header(labelOrient='top'))
)

heatmap with two years of data and labels on top

One solution is to remove the row specification and set the facet to have a single column

import altair as alt
from vega_datasets import data

df = data.seattle_weather()

alt.Chart(df).mark_rect().encode(
    alt.Y('month(date):O', title='day'),
    alt.X('date(date):O', title='month'),
    color='temp_max:Q'
).facet('year(date):N', columns=1)

enter image description here

Related