Search code examples
altairvega-litevega

altair, set axis step to 1d


This is a simple altar viz:

alt.Chart(df).mark_area().encode(
    x="date:T",
    y="value:Q",
)

viz

I would like to have all days on X axis. On viz you can see Sat 3, Wed 07 but I would like to have Thu 01, Fri 02, Sat 03 and so on.

( end of the question )


I put here the whole minimal reproducible sample:

import altair as alt
import pandas as pd

# random data
l = [ 3,4,6,7,8,9,12,34,54,65,56,76,87,77,90 ] 
data ={
    'date': [ f'2022-09-{d:02d}' for d in range(1,31) ],
    'value': l + list(reversed(l)),
}
df = pd.DataFrame(data)

# viz
alt.Chart(df).mark_area().encode(
    x="date:T",
    y="value:Q",
)

Also, I write here that I have tried. I have tried unsuccessfully with several options like to include format, change angle or change size.

viz with some axis options like format


Solution

  • I think something like the following:

    enter image description here

    import altair as alt
    import pandas as pd
    
    # random data
    l = [ 3,4,6,7,8,9,12,34,54,65,56,76,87,77,90 ] 
    data ={
        'date': [ f'2022-09-{d:02d}' for d in range(1,31) ],
        'value': l + list(reversed(l)),
    }
    df = pd.DataFrame(data)
    
    # viz
    alt.Chart(df).mark_area().encode(
        
        x=alt.X('date:O', axis=alt.Axis(format='' , labelExpr='date(datum.label) + " " + dayAbbrevFormat(day(datum.label))' ,title='date')),
        y="value:Q",
    )