简体   繁体   English

在 Altair 中更改单一颜色 plot

[英]Change single color in Altair plot

Is there a way to change the color of just a single group in Altair plot, while leaving the rest the same?有没有办法改变 Altair plot 中一个组的颜色,同时让 rest 保持不变? 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.我想在不更改 rest 的情况下将“versicolor”的颜色更改为黑色。

I know from the documentation that you can specify your own color scheme, or assign colors to each one of the groups, like so:我从文档中知道您可以指定自己的配色方案,或者将 colors 分配给每个组,如下所示:

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

example_scatterplot_2

But I cannot find a simple way to change the color for a single species without affecting the rest of the colors.但是我找不到一种简单的方法来改变单个物种的颜色而不影响 colors 的 rest。

One way to do this is with an alt.condition , although the updated color will not appear in the legend:一种方法是使用alt.condition ,尽管更新后的颜色不会出现在图例中:

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.如果您想更改颜色并将其反映在图例中,唯一的方法就是像您在问题中所做的那样使用自定义比例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM