pythonchartsaltair

Altair layered chart (bars and lines) cannot change color of the lines


I'm using Altair to create a layered chart with bars and lines. In this simple example, I'm trying to make the strokedash with different colors without success. I have tried using scale scheme for the strokedash, but it didn't change the colors.

Can anyone help?

import altair as alt
import pandas as pd

data = {
    'Date': ['2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02'],
    'Value': [50, 200, 300, 150, 250, 350, 200, 200, 200, 200, 200, 200, 300, 300, 300, 300],
    'DESCR': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'D', 'D', 'D', 'D'],
    'Company': ['X', 'X', 'Y', 'Y', 'X', 'X', 'Y', 'Y', 'X', 'X', 'Y', 'Y', 'X', 'X', 'Y', 'Y'],
}

source = pd.DataFrame(data)
source_aux = source[(source['DESCR']=='C') | (source['DESCR']=='D')]
source = source[(source['DESCR']=='A') | (source['DESCR']=='B')]

bars = alt.Chart().mark_bar().encode(
    x='Date:N',
    y='Value:Q',
    color=alt.Color('DESCR:N', legend=alt.Legend(title=None))
)

limits = alt.Chart(source_aux).mark_rule().encode(
    y='Value',
    strokeDash=alt.StrokeDash('DESCR', legend=alt.Legend(title=None))
)

chart = alt.layer(
    bars,
    limits,
    data=source
).facet(
    column='Company:N'
)

chart

Solution

  • strokeDash controls the dash of the line but has no impact on the color. To change the color of the line you can either use the stroke or color encoding.

    enter image description here

    import altair as alt
    import pandas as pd
    
    data = {
        'Date': ['2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02', '2023-01-01', '2023-01-02'],
        'Value': [50, 200, 300, 150, 250, 350, 200, 200, 200, 200, 200, 200, 300, 300, 300, 300],
        'DESCR': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'D', 'D', 'D', 'D'],
        'Company': ['X', 'X', 'Y', 'Y', 'X', 'X', 'Y', 'Y', 'X', 'X', 'Y', 'Y', 'X', 'X', 'Y', 'Y'],
    }
    
    source = pd.DataFrame(data)
    source_aux = source[(source['DESCR']=='C') | (source['DESCR']=='D')]
    source = source[(source['DESCR']=='A') | (source['DESCR']=='B')]
    
    bars = alt.Chart().mark_bar().encode(
        x='Date:N',
        y='Value:Q',
        color=alt.Color('DESCR:N', legend=alt.Legend(title=None))
    )
    
    limits = alt.Chart(source_aux).mark_rule().encode(
        y='Value',
        strokeDash=alt.StrokeDash('DESCR', legend=alt.Legend(title=None)),
        stroke=alt.Stroke('DESCR', legend=alt.Legend(title=None))
    )
    
    chart = alt.layer(
        bars,
        limits,
        data=source
    ).facet(
        column='Company:N'
    )
    
    chart