How to get the color code from a legend/selection

Viewed 49

I would like to use the color code, e.g. #262730 from an altair object for later usage in html code outside the chart.

E.g the following code creates a legend beneath my main chart:

chart = alt.Chart(df).mark_point().encode(
    y=alt.Y(f'{color_item}:N', axis=alt.Axis(orient='right')),
    color=alt.Color(f'{color_item}:N',)
)

enter image description here

Later on I would like to use the color for a heading, e.g.:

<h2 style="color:262730#";>%util</h2>

Is there any way to achieve that?

1 Answers

There is no way to directly access color codes in Altair from Python. Colors are determined by the vega/vega-lite library, which runs on the frontend in Javascript.

Your best option is probably to determine the scheme you're using and match it up with the palette definitions in the Vega-Lite javascript source: https://github.com/vega/vega/blob/v5.21.0/packages/vega-scale/src/palettes.js

Related