Altair date color scale range cuts off

Viewed 480

When using a date field to encode the color, the range of the color scale does not span the entire color scheme gradient range. For example, this:

alt.Chart(mydf).mark_point().encode(
    x='date:T',
    y='val:Q',
    color=alt.Color('date:T', scale=alt.Scale(scheme='yellowgreenblue'))
)

produces this chart:

enter image description here

Notice the color range does not go from yellow to blue, but is cut off at both ends. In contrast, if I use the val field (which is a float) to color, like this:

alt.Chart(mydf).mark_point().encode(
    x='date:T',
    y='val:Q',
    color=alt.Color('val:Q', scale=alt.Scale(scheme='yellowgreenblue'))
)

the color bar looks much better:

enter image description here

1 Answers

This appears to be a bug in Vega/Vega-Lite; I can't find any reports of it elsewhere. You can work around the issue by specifying the extent for the scheme. Usually, extent=[0, 1] (the default) would indicate using the full color scheme. Through some trial and error I found that extent=[-1, 2] seems to work well in this case:

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

mydf = pd.DataFrame({
  'date': pd.date_range('2020-01-01', '2020-06-15', periods=1000),
  'val': abs(np.random.randn(1000))
})

alt.Chart(mydf).mark_point().encode(
    x='date:T',
    y='val:Q',
    color=alt.Color('date:T', scale=alt.Scale(scheme=alt.SchemeParams(name='yellowgreenblue', extent=[-1, 2])))
)

enter image description here

You might consider reporting this on the Altair or Vega-Lite issue trackers.

Related