Search code examples
altair

How to apply color scale by field range?


In vega-lite I'm able to apply color scale by field range, I'm able to use the field values of color field to color my chart (this is the sample code).

I'm not able to rewrite in Python/altair, the same of this JSON.

"color": {
  "type": "nominal",
  "field": "label",
  "scale": {"range": {"field": "color"}},
  "title": ""
}

It's not this

chart=alt.Chart(df).mark_rect().encode(
    x='x:O',
    y='y:O',
    color=alt.Color('label:N',scale=range('color:N'))
)

Solution

  • It's color=alt.Color('label:N').scale(range={"field": "color"})

    I have had the reply in altair discussions (thank you Christopher Davis)