pythonaltair

Multiple line figure without new coloring in Altair


The basic example of multiple lines in Altair shows distinct lines in a different color:

import altair as alt
from vega_datasets import data

source = data.stocks()

alt.Chart(source).mark_line().encode(
    x='date:T',
    y='price:Q',
    color='symbol:N',
)

How does one make this same figure but using the same colour for each line and ignoring their labels (for instance when you have replicates of a model)?

I don't want them faceted (on different panels) and I would like this scalable for 100s of different groups (such as in simulation replicates).


Solution

  • The trick is to use the detail channel rather than the color channel. For your example, I believe that this produces what you're looking for:

    import altair as alt
    from vega_datasets import data
    
    source = data.stocks()
    
    alt.Chart(source).mark_line().encode(
        x='date:T',
        y='price:Q',
        detail='symbol:N',
    )
    

    The result:

    same color, no label