It is easy to use a color scheme (for example set2
) in each Altair diagram:
import pandas as pd
import altair as alt
from sklearn.datasets import load_iris
data_iris = load_iris()
df = pd.DataFrame(data_iris["data"], columns=data_iris["feature_names"])
df["species"] = [data_iris["target_names"][i] for i in data_iris["target"]]
alt.Chart(df).mark_circle().encode(
x=alt.X("sepal width (cm)", scale=alt.Scale(zero=False)),
y=alt.Y("sepal length (cm)", scale=alt.Scale(zero=False)),
color=alt.Color("species",
scale=alt.Scale(scheme="set2") ## should be in theme
)
)
I want to use the color theme for all diagrams, but I do not find a way to configure a color scheme as an Altair scheme.
import altair as alt
def my_theme():
return {"config": None} ## How can we set the color scheme here?
alt.themes.register('my_theme', my_theme)
alt.themes.enable('my_theme')
By reading Documentation , I tried several, but no success.
You will want to configure the range
parameter. See Scale Range Properties for more details.
import altair as alt
from vega_datasets import data
# define the theme by returning the dictionary of configurations
def my_theme():
return {
'config': {
'view': {
'height': 300,
'width': 400,
},
'range': {
'category': {'scheme':'set2'}
}
}
}
# register the custom theme under a chosen name
alt.themes.register('my_theme', my_theme)
# enable the newly registered theme
alt.themes.enable('my_theme')
# draw the chart
cars = data.cars.url
alt.Chart(cars).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color='Origin:N'
)
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.