alt.Chart()中标签的指定颜色

时间:2019-02-21 15:01:16

标签: python-3.x scatter-plot altair

比方说,我想用相应的颜色['foo', 'bar', 'test']绘制['blue', 'yellow', 'green']。我对解决这个问题的建议如下:

alt.Chart(df, height=600, width=1100).mark_circle(size=100).encode(
        x='x',
        y='y',
        color=alt.Color('title:N', legend=None, scale=alt.Scale(range=['blue', 'yellow', 'green'])),
        tooltip=['sample', 'title']
    ).properties(
        selection=click
    ).interactive()

但是,这似乎是按任意顺序选择颜色,而不是按我期望的升序排列。我希望为foo分配blue颜色,并且bar应该变成yellow,依此类推。

是否可以通过某种方式链接此(标签和颜色)?

1 个答案:

答案 0 :(得分:1)

Customizing Colors部分的文档中对此有一些信息。简而言之,如果您想控制将哪些值分配给哪种颜色,可以使用domain参数:

scale = alt.Scale(domain=['foo', 'bar', 'test'], range=['blue', 'yellow', 'green'])