How to apply color scale by field range? #2916
-
In vega-lite I'm able to apply color scale by field range, I'm able to use the field values of 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'))
) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
If you ever want to compare your Altair code to the Vega-Lite code, a good trick is to evaluate I believe this will work:
@mattijn Do you see a nicer way than explicitly specifying this dictionary, In a soon-to-be-released version of Altair, you can skip the
There is also an option to use the raw color values directly, but I couldn't figure out a nice way to also get your labels to show up with this approach: https://altair-viz.github.io/user_guide/customization.html#raw-color-values
|
Beta Was this translation helpful? Give feedback.
If you ever want to compare your Altair code to the Vega-Lite code, a good trick is to evaluate
print(chart.to_json())
, which can sometimes help with spotting the problem (maybe not as much in this particular example).I believe this will work:
@mattijn Do you see a nicer way than explicitly specifying this dictionary,
{"field": "color"}
?In a soon-to-be-released version of Altair, you can skip the
alt.Scale
portion:There is also an option to use the raw color values directly, but I couldn't figure out a nice way to also get your labels to show up with this ap…