1

我有一张c2源自(继承自)原始图表的图表c1

如何将颜色编码通道重置c2为空?

这是一个简化的例子

import altair as alt
import pandas as pd

data = pd.DataFrame({
    'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
    'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})

c1 = alt.Chart(data).mark_bar().encode(
    x='a',
    y='b',
    color='a'
)

c2 = c1.mark_text().encode(
    text='a')

c1 + c2

问题是我想c2成为一层黑色(非彩色)文本标签。

我尝试了以下选项但没有成功:

c2 = c1.mark_text().encode(
    text='a',
    color=None
)

c2 = c1.mark_text().encode(
    text='a',
    color=alt.Color(field=None)
)

c2 = c1.mark_text().encode(
    text='a',
    color=alt.Undefined
)
4

2 回答 2

2

Your solution works. I'm adding another option for future viewers who might stumble upon this question wanting to specify colors. It is to explicitly specify the text to be "black" using alt.value('black') Admittedly, 'black' is not the same as "unsetting" the encoding, but it could be what someone wants for their plot.

c2 = c1.mark_text().encode(
    text='a',
    color=alt.value('black')
)
于 2018-05-05T00:59:39.530 回答
2

c2您可以使用alt.Undefined如下方式重置颜色编码:

c2.encoding.color = alt.Undefined

于 2018-04-29T07:48:41.470 回答