Altair-绘制自定义颜色

时间:2020-06-16 01:59:19

标签: python altair

我有一些想用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.
        )

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

1 个答案:

答案 0 :(得分:2)

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

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.
)
相关问题