Is there a way to change the color of just a single group in Altair plot, while leaving the rest the same? I like the default color scheme fine, but there is one group I want to change the color for.
For example, in a scatter-plot like this:
import altair as alt
from vega_datasets import data
iris = data.iris()
alt.Chart(iris).mark_point().encode(
x='petalWidth',
y='petalLength',
color=alt.Color('species')
)
I would like to change the color of 'versicolor' to black, without changing the rest.
I know from the documentation that you can specify your own color scheme, or assign colors to each one of the groups, like so:
domain = ['setosa', 'versicolor', 'virginica']
range_ = ['red', 'green', 'blue']
alt.Chart(iris).mark_point().encode(
x='petalWidth',
y='petalLength',
color=alt.Color('species', scale=alt.Scale(domain=domain, range=range_))
)
But I cannot find a simple way to change the color for a single species without affecting the rest of the colors.
CodePudding user response:
One way to do this is with an alt.condition
, although the updated color will not appear in the legend:
import altair as alt
from vega_datasets import data
iris = data.iris()
alt.Chart(iris).mark_point().encode(
x='petalWidth',
y='petalLength',
color=alt.condition("datum.species == 'setosa'", alt.value('yellow'), alt.Color('species'))
)
If you want to change the color and have it reflected in the legend, the only way to do so is to use a custom scale as you did in your question.