1

In the below example, I create an bar plot, with the height and the color of the bars both encoding the y column of df. The bars then appear in different shades, for very light to very dark; in blue.

import altair as alt
import numpy as np
import polars as pl


N = 20
df = pl.DataFrame({
    'x': range(N),
    'y': np.random.randint(0, 100, N)
})
alt.Chart(df).mark_bar().encode(x='x', y='y', color='y')

Now, how do tell altair to use the same encoding but apply another color map of my liking, such that the bars are displayed in, e.g. shades of yellow?

Adding color=yellow to mark_bar does not work, as it is overwritten by the subsequent encoding.

1 Answer 1

2

'yellow' is not a valid color scheme in Altair. The valid color schemes listed include options like 'yellowgreen', 'yelloworangered', and 'yelloworangebrown', but not 'yellow'

Check here: https://vega.github.io/vega/docs/schemes/

Corrected Code:

import altair as alt
import polars as pl
import numpy as np

N = 20
df = pl.DataFrame({
    'x': range(N),
    'y': np.random.randint(0, 100, N)
})

alt.Chart(df).mark_bar().encode(
    x='x',
    y='y',
    color=alt.Color('y', scale=alt.Scale(scheme='yelloworangered'))
)

If you want a more specific yellow gradient, you can define a custom range:

alt.Chart(df).mark_bar().encode(
    x='x',
    y='y',
    color=alt.Color('y', scale=alt.Scale(range=['#FFFFE0', '#FFC107', '#FFA000']))
)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.