Search code examples
python-3.xaltairvega-lite

Python Altair how do I skip (or squeeze) part of the x axis?


Say I have data indexed by date time (pd.Timestamp type), but I know for certain that between certain times each day, there is no data (financial data for example), and I would like to plot data over several days but I want to skip the night times and only draw the day times, so that from one day to the next there isn't a huge gap (of night time) on the x-axis. Is it possible to do that?


Solution

  • You can use the same approach as in this answer for VegaLite (Vega-Lite - Skip week-end (non-business days) in temporal axis); using an ordinal axis with a timeUnit encoding:

    import altair as alt
    import pandas as pd
    
    
    records = [
          {"Date": "2020/08/10 10:00", "Close": 3170},
          {"Date": "2020/08/10 11:00", "Close": 3104.11},
          {"Date": "2020/08/10 12:00", "Close": 3134.05},
          {"Date": "2020/08/10 13:00", "Close": 3140.38},
          {"Date": "2020/08/10 14:00", "Close": 3138.15},
          {"Date": "2020/08/10 15:00", "Close": 3149.16},
          {"Date": "2020/08/10 16:00", "Close": 3149.87},
          {"Date": "2020/08/10 17:00", "Close": 3137.6},
          {"Date": "2020/08/11 10:00", "Close": 3121},
          {"Date": "2020/08/11 11:00", "Close": 3123.5},
          {"Date": "2020/08/11 12:00", "Close": 3135.33},
          {"Date": "2020/08/11 13:00", "Close": 3143.11},
          {"Date": "2020/08/11 14:00", "Close": 3135.1},
          {"Date": "2020/08/11 15:00", "Close": 3121.76},
          {"Date": "2020/08/11 16:00", "Close": 3080.67},
          {"Date": "2020/08/11 17:00", "Close": 3085.99},
          {"Date": "2020/08/12 10:00", "Close": 3151.62},
          {"Date": "2020/08/12 11:00", "Close": 3143.74},
          {"Date": "2020/08/12 12:00", "Close": 3152.53},
          {"Date": "2020/08/12 13:00", "Close": 3153.75},
          {"Date": "2020/08/12 14:00", "Close": 3167.69},
          {"Date": "2020/08/12 15:00", "Close": 3158.09},
          {"Date": "2020/08/12 16:00", "Close": 3158.7},
          {"Date": "2020/08/12 17:00", "Close": 3160.51},
          {"Date": "2020/08/13 10:00", "Close": 3181},
          {"Date": "2020/08/13 11:00", "Close": 3201.73},
          {"Date": "2020/08/13 12:00", "Close": 3209.34},
          {"Date": "2020/08/13 13:00", "Close": 3195.99},
          {"Date": "2020/08/13 14:00", "Close": 3198.41},
          {"Date": "2020/08/13 15:00", "Close": 3181.06},
          {"Date": "2020/08/13 16:00", "Close": 3170.61},
          {"Date": "2020/08/13 17:00", "Close": 3161.02},
    ]
    alt.Chart(pd.DataFrame(records)).mark_line(point=True).encode(
        x=alt.X('Date:O', timeUnit="yearmonthdatehours", axis=alt.Axis(labelAngle=-45)),
        y=alt.Y('Close', scale=alt.Scale(zero=False))
    )
    

    enter image description here

    This is what it would have looked like on a temporal axis, note the long connecting lines without dots which are the night periods:

    alt.Chart(pd.DataFrame(records), width=650).mark_line(point=True).encode(
        x=alt.X('yearmonthdatehours(Date):T', axis=alt.Axis(labelAngle=-45)),
        y=alt.Y('Close', scale=alt.Scale(zero=False))
    )
    

    enter image description here