Altair - 绘制自定义颜色

Altair - plotting custom colors

我有自定义颜色,我想用 altair 绘制。

说,color = '#CE1317'

bar = alt.Chart(df).mark_bar().encode(
        alt.X('Performance Indicator:N', title=""),
        alt.Y(f'{player[0]}:Q', title="", sort='-x', scale=alt.Scale(domain=(0.0, 2.5))),
        color=f'{color}:N',
        tooltip=[f'{player[0]}:Q']
        ).properties(
        height=600,
        width=alt.Step(40)  # controls width of bar.
        )

但这不起作用。它绘制蓝色。可能吗?

是的,您可以使用值编码来做到这一点:

bar = alt.Chart(df).mark_bar().encode(
    alt.X('Performance Indicator:N', title=""),
    alt.Y(f'{player[0]}:Q', title="", sort='-x', scale=alt.Scale(domain=(0.0, 2.5))),
    color=alt.value(color),
    tooltip=[f'{player[0]}:Q']
).properties(
    height=600,
    width=alt.Step(40)  # controls width of bar.
)