在 Altair 图表中设置颜色

Setting colors in Altair charts

我正在尝试创建一个图表来跟踪 19 个不同的数据列,并希望为每个不同的层设置一个预定义的颜色代码。

base = alt.Chart(historical_WTI.loc['0': '220'].reset_index(), 
                 title=f"WTI Futures (as of {last_date})").mark_line().encode(
    x = alt.X('days_to_expiry', title = 'Days to Expiry'),
    y = alt.Y(alt.repeat('layer'), scale=alt.Scale(zero = False), 
              type="quantitative", title = 'Open Interest'), 
    color = alt.Color('Key:O', scale = alt.Scale(domain = WTI_securities, range = color_list))
).repeat(layer = historical_WTI.columns.values.tolist())
base

WTI_securities 和 color_list 都是按照我的映射所需的顺序排列的列表,但我返回以下内容 chart

当我注释掉颜色线时,我可以得到以下内容 chart 但我需要颜色映射和图例。

有人知道我做错了什么吗?

不知道这是否回答了你的问题,但我从这个例子开始:
https://altair-viz.github.io/gallery/line_chart_with_color_datum.html
添加 configure_range.

import altair as alt
from vega_datasets import data

source = data.movies()

colors = ['#7fc97f','#beaed4','#fdc086']

alt.Chart(source).mark_line().encode(
    x=alt.X("IMDB_Rating", bin=True),
    y=alt.Y(
        alt.repeat("layer"), aggregate="mean", title="Mean of US and Worldwide Gross"
    ),
    color=alt.datum(alt.repeat("layer")),
).repeat(
    layer=["US_Gross", "Worldwide_Gross", "Production_Budget"],
).configure_range(
    category=alt.RangeScheme(colors)
)

这给了我这个: