Search code examples
altair

How to hide subplot labels in facet graph?


This code:

import altair as alt
from vega_datasets import data

alt.Chart(data.iris()).mark_point().encode(
    x='petalLength:Q',
    y='petalWidth:Q',
    facet=alt.Facet('species:N', header=alt.Header(labels=False, title=None), columns=3)
).properties(
    width=250,
    height=250
)

produces this chart:

chart

I have added red lines beneath what I believe are called subplot "labels". How can I remove them? This question asked how to change them, and @jakevdp said that is impossible. This question asked how to remove them (same as me), and @jakevdp hasn't answered yet. They used a column parameter to eliminate the labels, but when I try that, it complains about the columns parameter I'm using. I want to both (a) eliminate the labels and (b) limit the number of subplots per row, with automatic row wrapping.


Solution

  • What you did should have worked (i.e. alt.Header(title=None, labels=False)); the fact that it doesn't is probably a bug (I think it's the same issue reported here: https://github.com/altair-viz/altair/issues/2252)

    As a workaround for the issue, you can use labelExpr to hide them:

    alt.Chart(data.iris()).mark_point().encode(
        x='petalLength:Q',
        y='petalWidth:Q',
        facet=alt.Facet('species:N', header=alt.Header(labelExpr="''", title=None), columns=3)
    ).properties(
        width=250,
        height=250
    )