如何在 altair 中将自定义颜色主题设置为默认?

How to set a custom color theme in altair as default?

我想在 altair 中创建并注册一个颜色主题,其中包含我选择的 10 种颜色,并将其用作默认颜色主题。我找不到办法做到这一点。我在这里尝试了 johnmellor 描述的方式 https://github.com/altair-viz/altair/issues/268 但没有任何改变。

您可以使用 chart.configure_range() 为各种类型的色标设置默认方案。这是一个例子:

import altair as alt
from vega_datasets import data

source = data.cars.url

alt.Chart(source).mark_circle(size=60).encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color='Origin:N',
).configure_range(
    category={'scheme': 'dark2'}
)

如果您想通过主题而不是图表上的配置来执行此操作,它将如下所示:

def my_theme():
  return {
    'config': {
      'view': {'continuousHeight': 300, 'continuousWidth': 400},  # from the default theme
      'range': {'category': {'scheme': 'dark2'}}
    }
  }
alt.themes.register('my_theme', my_theme)
alt.themes.enable('my_theme')

alt.Chart(source).mark_circle(size=60).encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color='Origin:N',
)

可在此处找到可用配色方案列表:https://vega.github.io/vega/docs/schemes/#reference