Oleg
Oleg

Reputation: 191

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

While specifying domain starting at zero:

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

I still obtain a plot with negative values on the X axis: enter image description here

I don't understand why is it behaving like this? And how to force it starting always exactly at the value, provided in the domain?

Code for plotting:

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")

Lib version: altair 3.2.0

Upvotes: 1

Views: 303

Answers (1)

jakevdp
jakevdp

Reputation: 86433

For bar marks, Vega-Lite automatically adds a padding to domains (this is not the case for other mark types). The fact that it does this even when the user explicitly specifies the domain is a bug; see vega/vega-lite#5295.

As a workaround until this bug is fixed, you can turn this behavior offby setting 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")
)

enter image description here

Upvotes: 1

Related Questions