Show more timestamp ticks in altair

Viewed 20

Here is a toy example of my data along with the code I am working with

import altair as alt
import numpy as np
import pandas as pd

np.random.seed(0)
df = pd.DataFrame({
        'grp': np.random.choice(['A','B'],100, p=[0.5,0.5]),
        'dur': np.random.randint(350, 1250, 100),
    })
alt.Chart(df).mark_boxplot().encode(
    x=alt.X('grp:N', title='Group'),
    y=alt.Y('dur:T', title='Duration', axis=alt.Axis(format="%S.%L", tickCount=5)), 
).properties(width=100)

I am getting only two ticks on the Y-axis, i.e. 0.800, 1.200 which is in seconds:milliseconds format. I am unable to include the resulting plot because of insufficient reputation. I want smaller time steps in milliseconds the axis ticks such as 0.200, 0.400, 0.600, 0.800, 1.000, 1.200. Can someone please help me get it?

The tickCount option doesn't change anything.

1 Answers

It seems like neither of tickCount, values, or alt.Scale(zero=True) work as expected with a temporal axis, but if you are ok with changing the axis type to quantitative and dividing your input values, you can get this plot:

import altair as alt
import numpy as np
import pandas as pd

np.random.seed(0)
df = pd.DataFrame({
        'grp': np.random.choice(['A','B'],100, p=[0.5,0.5]),
        'dur': np.random.randint(350, 1250, 100) / 1000,
    })
alt.Chart(df).mark_boxplot().encode(
    x=alt.X('grp:N', title='Group'),
    y=alt.Y('dur:Q', title='Duration', axis=alt.Axis(format=".3f")), 
).properties(width=100)

enter image description here

Related