I didn't see a way to do this in the spec. I'd be happy to write some documentation if someone could explain how to do this.
For example, how can I ensure that black is used for time_of_day == 'night' and gold for time_of_day == 'morning'?
Chart(data).mark_area().encode(
color='time_of_day:N',
x='frame:Q',
y='movement:Q',
)
You can do this by specifying the Scale for the color encoding. There's an example near the end of the Seattle Weather Tutorial.
For your chart, it might look something like this:
from altair import Chart, Color, Scale
Chart(data).mark_area().encode(
Color('time_of_day:N',
scale=Scale(domain=['night', 'morning'],
range=['black', 'gold'])),
x='frame:Q',
y='movement:Q',
)
If you would like to add a section on this in the config documentation, that would be a nice contribution!
It might also be nice to document how to set the default color scheme. It seems the following is a minimal example for how to change the default color scheme to category10:
my_theme = alt.themes.get()() # Get current theme as dict.
my_theme.setdefault('encoding', {}).setdefault('color', {})['scale'] = {
'scheme': 'category10',
}
alt.themes.register('my_theme', lambda: my_theme)
alt.themes.enable('my_theme')
And similarly to set a custom categorical scheme as default:
my_theme = alt.themes.get()() # Get current theme as dict.
my_theme.setdefault('encoding', {}).setdefault('color', {})['scale'] = {
'range': [
# Start with category10 color cycle:
'#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd',
'#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf',
# Then continue with the paired lighter colors from category20:
'#aec7e8', '#ffbb78', '#98df8a', '#ff9896', '#c5b0d5',
'#c49c94', '#f7b6d2', '#c7c7c7', '#dbdb8d', '#9edae5',
],
}
alt.themes.register('my_theme', lambda: my_theme)
alt.themes.enable('my_theme')
Just wanted to document an almost identical way, but aligned with what's in the docs.
tableau10 can be any of these: https://vega.github.io/vega/docs/schemes/#categorical
# Define your custom theme.
def my_theme():
return {
'config': {
'range': {'category': {'scheme': 'tableau10'}}
}
}
# Register and enable.
alt.themes.register('my_theme', my_theme)
alt.themes.enable('my_theme')
Is there a way to register a scheme to set as default? To be able to use it with the solution @johnmellor suggested for changing the theme but with a custom scheme instead of list of colors?
It might also be nice to document how to set the default color scheme. It seems the following is a minimal example for how to change the default color scheme to category10:
my_theme = alt.themes.get()() # Get current theme as dict. my_theme.setdefault('encoding', {}).setdefault('color', {})['scale'] = { 'scheme': 'category10', } alt.themes.register('my_theme', lambda: my_theme) alt.themes.enable('my_theme')And similarly to set a custom categorical scheme as default:
my_theme = alt.themes.get()() # Get current theme as dict. my_theme.setdefault('encoding', {}).setdefault('color', {})['scale'] = { 'range': [ # Start with category10 color cycle: '#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf', # Then continue with the paired lighter colors from category20: '#aec7e8', '#ffbb78', '#98df8a', '#ff9896', '#c5b0d5', '#c49c94', '#f7b6d2', '#c7c7c7', '#dbdb8d', '#9edae5', ], } alt.themes.register('my_theme', lambda: my_theme) alt.themes.enable('my_theme')
This method didn't work for me to set my color theme as default. The default altair color theme was still active.
Most helpful comment
Just wanted to document an almost identical way, but aligned with what's in the docs.
tableau10can be any of these: https://vega.github.io/vega/docs/schemes/#categorical