Reputation: 561
So I know that you can select colors in an Altair plot by setting color='blue'
or by specifying a hex code. I was wondering if it's possible to instead use a single color from a Vega color scheme like dark2 or set1 without explicitly specifying its hex code.
I want it be something like:
color = sample_from_scheme('set1')
alt.Chart(df).mark_circle().encode(
x='X',
y='Y',
color=color
).save(
"image.html"
)
Upvotes: 2
Views: 2817
Reputation: 48879
You could access a single color from the Vega color schemes by parsing the color scheme source file from the Vega repo:
color_scheme = 'set1'
color_number = 0
color = pd.read_table(
'https://raw.githubusercontent.com/vega/vega/v5.21.0/packages/vega-scale/src/palettes.js',
skipinitialspace=True,
sep=':',
).loc[
color_scheme
].str.replace(
"'",
""
).str.replace(
",",
""
).apply(
lambda x: ["#" + x[i:i+6] for i in range(0, len(x), 6)]
)[0][color_number]
color
Output:
'#e41a1c'
You can see all colors by going to https://vega.github.io/vega/docs/schemes/ and hovering the names in case you want to confirm.
Upvotes: 2
Reputation: 86310
Scheme names are evaluated in the Javascript renderer; there is currently no way to access the colors represented by scheme names from within Python.
You can see where the color schemes are defined in Vega's javascript source here: https://github.com/vega/vega/blob/v5.21.0/packages/vega-scale/src/palettes.js
So, for example, set1
consists of ['e41a1c', '377eb8', '4daf4a', '984ea3', 'ff7f00', 'ffff33', 'a65628', 'f781bf', '999999']
Upvotes: 5