轴的域不完全定义图表的呈现方式

Domain of the axes does not exactly defines how chart is rendered

指定从零开始的域时:

alt.Scale(domain=(0, 1000))

我仍然得到 X 轴上有负值的图:

我不明白为什么会这样?以及如何强制它始终准确地从域中提供的值开始?

绘图代码:

data=pd.DataFrame({'foo': {0: 250,
  1: 260,
  2: 270,
  3: 280,
 },
 'cnt': {0: 6306,
  1: 5761,
  2: 5286,
  3: 4785,
 }})


alt.Chart(data).mark_bar().encode(
        alt.X(
            'foo',
            scale=alt.Scale(domain=(0, 1000))
        ),
        alt.Y("cnt")

库版本: 牵牛星 3.2.0

对于条形标记,Vega-Lite 会自动向域添加填充(其他标记类型不是这种情况)。事实上,即使用户明确指定域,它也会这样做,这是一个错误;参见 vega/vega-lite#5295

作为解决此错误之前的解决方法,您可以通过设置 padding=0:

来关闭此行为
import altair as alt
import pandas as pd

data=pd.DataFrame({
    'foo': [250, 260, 270, 280],
    'cnt': [6306, 5761, 5286, 4785]
})


alt.Chart(data).mark_bar().encode(
    alt.X(
        'foo',
        scale=alt.Scale(domain=(0, 1000), padding=0)
    ),
    alt.Y("cnt")
)