Skip to content

Generating colors from data, by RGB channels #2956

Answered by binste
dcbb asked this question in Q&A
Discussion options

You must be logged in to vote

It is possible using so called expressions, see Expressions for interaction for an introduction. ChatGPT was onto something but it's not quite correct :)

import altair as alt
import pandas as pd

df = pd.DataFrame(
    [[1, 1, 102, 204, 0], [2, 2, 0, 0, 255], [3, 3, 0, 0, 0]],
    columns=["x", "y", "r", "g", "b"],
)

alt.Chart(df).mark_circle(size=200).encode(
    x="x",
    y="y",
    # In Altair 5 you will be able to replace alt.ExprRef with alt.expr
    color=alt.value(alt.ExprRef(alt.expr.rgb(alt.datum.r, alt.datum.g, alt.datum.b))),
    # Or you can write the expression as a string like this
    # color=alt.value(alt.ExprRef("rgb(datum.r, datum.g, datum.b)")),
)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@dcbb
Comment options

Answer selected by dcbb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants