Search code examples
pythondata-sciencedata-visualizationaltair

Avoid X axis labels repetition in Altair chart


So I got this chart which displays a time series in X.Axis using yearmonth('date'):T. The thing is, it duplicates the labels to fill the spaces, but I don't want that behaviour. I'd like a single label each time. How can I do this?

The chart

example df:
ID      date        total
3425    2022-01-04  30
15161   2022-01-12  1730
18192   2022-01-13  1526
21014   2022-01-21  99
24692   2022-02-07  28
30163   2022-02-21  175
31531   2022-02-22  446
34467   2022-03-22  57
34880   2022-03-23  500
35144   2022-03-30  41

Solution

  • Try to change the type of the date column to O (Ordinal) if you are using T(Time).

    chart = (
    alt.Chart(source).mark_line().encode(
        x=alt.X("Fecha:O", timeUnit="yearmonth", title="Fecha"),
        y=alt.Y("Monto:Q", stack=None)
    )