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

Viewed 2892

I would like to create and register a color theme in altair with 10 colors of my choice and use it as a default color theme. I cannot find a way to do this. I tried the way described by johnmellor here https://github.com/altair-viz/altair/issues/268 but nothing has changed.

1 Answers

You can set default schemes for various types of color scales using chart.configure_range(). Here is an example:

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'}
)

enter image description here

If you want to do this via a theme rather than a config on the chart, it would look like this:

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',
)

enter image description here

The list of available color schemes can be found here: https://vega.github.io/vega/docs/schemes/#reference

Related