Search code examples
pythondata-visualizationaltair

Altair line chart adds unwanted extra day at the beginning


I'm creating a line chart using the data shown in the dataframe. When I do it, Altair counts some of the data in the day prior to the actual starting date. How can I avoid this?

Here is the code that generates the chart:

data = df
x_val = 'monthdate(creationTime):T'
y_val = 'count(total):Q'

def line_chart(data, title_chart, x_val, y_val, title_x, title_y):

    lines = (
        alt.Chart(
            data,
            title=title_chart,
        )
        .mark_line(point=alt.OverlayMarkDef())
        .encode(
            x=alt.X(
                x_val,
                title=title_x,
                axis=alt.Axis(grid=False),
            ),
            y=alt.Y(
                y_val,
                title=title_y,
                axis=alt.Axis(grid=False)
            ),
        )
    )

    hover = alt.selection_single(
        fields=[x_val],
        nearest=True,
        on="mouseover",
        empty="none",
    )

    points = lines.transform_filter(hover).mark_circle(size=65)

    tooltips = (
        alt.Chart(data)
        .mark_rule()
        .encode(
            x=x_val,
            y=y_val,
            opacity=alt.condition(hover, alt.value(0.3), alt.value(0)),
            tooltip=[
                alt.Tooltip(x_val, title=title_x),
                alt.Tooltip(y_val, title=title_y),
            ],
        )
        .add_selection(hover)
    )

    final = (lines + points + tooltips).configure_view(strokeWidth=0).interactive()
    return st.altair_chart(final, use_container_width=True)

Altair screenshot


Solution

  • I managed to fix this using df.dt.tz_localize(). Apparently Altair thinks the dates are UTC by default.