如何在 Altair 中将轴的域设置为不是 5 的倍数的值?

How do i set the domain of an axis to a value that isn't a multiple of five in Altair?

我正在尝试将 x 轴域设置为 0-36 之间,因为我正在处理的一些数据是以 6 周为增量收集的。根据文档,我使用了 scale=alt.Scale(domain=[0,36])。但是,这会继续显示最多 40 个图表。

df = pd.DataFrame({'x':[0,6,12,18,24,30,36],'y':[0,3,1,4,2,5,3]})
alt.Chart(df).mark_line(point=True).encode(
    x=alt.X('x:Q',
            axis=alt.Axis(values=[0,6,12,18,24,30,36]),
            scale=alt.Scale(domain=[0,36])),
    y=alt.Y('y:Q'),
)

Output of code above

将上述代码更改为在 30 和 35 之间截断,即 scale=alt.Scale(domain=[0,31]) 生成 this behavior,,其中图表轴在 30 处被截断(但显示 30 之后的数据,适当地因为数据没有'被剪掉了)。

但为什么我不能在不是 5 的倍数的值处截断图表?

我正在使用 Altair v4.0.1

Vega-Lite 渲染器默认选择“合适”的比例值。如果你想禁用这个行为,你可以通过 nice=False:

import pandas as pd
import altair as alt

df = pd.DataFrame({'x':[0,6,12,18,24,30,36],'y':[0,3,1,4,2,5,3]})
alt.Chart(df).mark_line(point=True).encode(
    x=alt.X('x:Q',
            axis=alt.Axis(values=[0,6,12,18,24,30,36]),
            scale=alt.Scale(domain=[0,36], nice=False)),
    y=alt.Y('y:Q'),
)