Search code examples
chartsaltair

Remove blank lines in Gantt chart when using Facets in altair


The following code:

import altair as alt
import pandas as pd

source = pd.DataFrame([
    {"task": 1, "start": 1, "end": 3, "group": 1},
    {"task": 2, "start": 3, "end": 8, "group": 1},
    {"task": 3, "start": 8, "end": 10, "group": 1},
    {"task": 1, "start": 1, "end": 3, "group": 2},
    {"task": 2, "start": 3, "end": 8, "group": 2},
    {"task": 3, "start": 8, "end": 10, "group": 2},
    {"task": 4, "start": 10, "end": 13, "group": 2},
    {"task": 5, "start": 13, "end": 14, "group": 2},
])


chart = alt.Chart(source).mark_bar().encode(
    x=alt.X('start', type="quantitative"),
    x2=alt.X2('end'),
    y=alt.Y('task', type="ordinal"),
    facet=alt.Facet("group", type="nominal", columns=1)
)

produces the following Gantt chart grouped by value of column group using alt.Facet: image

Is there a way to drop the blank lines from sub-plot of group 1 that correspond to lines number 4 and 5? I want to keep just filled lines of the chart.


Solution

  • You can resolve the y-scale to be independent of the other charts in the spec:

    alt.Chart(source).mark_bar().encode(
        x=alt.X('start', type="quantitative"),
        x2=alt.X2('end'),
        y=alt.Y('task', type="ordinal"),
        facet=alt.Facet("group", type="nominal", columns=1)
    ).resolve_scale(
        y='independent'
    )
    

    enter image description here