简体   繁体   中英

Altair add selector to change color scheme

I have a simple plot withe the color scheme dark2

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

在此处输入图像描述

I'd like to add a dropdown selector that allows the user to switch between dark2 and accent. I've tried a variety of different approaches but nothing seems to be correct. Any help is greatly appreciated.

Was able to figure out the solution by using the approach below

import altair as alt
from vega_datasets import data

cars = data.cars()
input_dropdown = alt.binding_select(options=['accent','dark2'], name='Color Scheme')
param = alt.Parameter(name="schemeselect", value = "accent", bind = input_dropdown)

alt.Chart(cars,params = [param]).mark_circle(size=60).encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color=alt.Color('Origin', scale=alt.Scale(scheme={"expr": "schemeselect"})),
)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM