Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change facet title position in Altair?

Tags:

python

altair

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

like image 716
max Avatar asked Sep 06 '25 13:09

max


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

like image 55
C. Braun Avatar answered Sep 09 '25 05:09

C. Braun


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

like image 42
eitanlees Avatar answered Sep 09 '25 04:09

eitanlees