scapi
scapi

Reputation: 41

Can an Altair compound chart (layered and concatenated) have different background colours?

Similar to the question here: Can an Altair facet chart have alternating background colors?

Is there an example to demonstrate having different backgrounds for concatenated charts? I am creating a complex compound chart which consists of many layered charts which are concatenated horizontally and vertically.

Having in mind the altair documentation about "Customizing Visualizations" (global versus local encodings) and "Top-Level Chart Configuration" it seems still unclear how to accomplish via a configuration.

The only way I could solve this was by layering another mark_rect with the specific color and opacity before concatenation.

Upvotes: 1

Views: 491

Answers (1)

joelostblom
joelostblom

Reputation: 48879

I don't think it is possible to concatenate charts with either a background or configure object. A workaround could be to create a rectangle mark manually in the background layer:

import altair as alt
from vega_datasets import data

source = data.cars()
chart = alt.Chart(source).mark_circle().encode(
    x='Horsepower',
    y='Miles_per_Gallon',
)

bg = chart.mark_rect(color='coral').encode(
    x=alt.value(0),
    y=alt.value(0),
    x2=alt.value(400),
    y2=alt.value(300))

(bg + chart) | (bg.mark_rect(color='gold') + chart)

enter image description here

Upvotes: 1

Related Questions