Altair: layered charts can't seem to have different color mappings

Created on 17 Jul 2018  路  4Comments  路  Source: altair-viz/altair

It looks like layered charts can't have different color scales. Is that true? Intentional?

I'm using a different color scale for the bars as the lines, and then composing the charts. My goal is to have a slightly lighter color scheme for the lines than the bars, to see what that might look like. But, it doesn't seem to be respecting the second colorscale.

To test it, I did something more obvious: using "red" instead of "lightgrey".

See code and image below.


import altair as alt
from vega_datasets import data

iowa = data.iowa_electricity()

bars = alt.Chart(iowa).mark_bar().encode(
    x="year:T",
    y=alt.Y("net_generation:Q", stack="normalize"),
    color=alt.Color("source:N",
                    scale=alt.Scale(domain=['Fossil Fuels', 'Nuclear Energy', 'Renewables'],
                      range=['lightgrey', 'steelblue', 'blue']))
)

lines = alt.Chart(iowa).mark_line().encode(
    x="year:T",
    y=alt.Y("net_generation:Q", stack="normalize"),
    color=alt.Color("source:N",
                    scale=alt.Scale(domain=['Fossil Fuels', 'Nuclear Energy', 'Renewables'],
                      range=['red', '#94caf7', '#6b6bff']))
)

alt.layer(
    bars,
    lines
)

6sj7sejvvps

question

Most helpful comment

Altair syntax looks like this:

alt.layer(
    bars,
    lines
).resolve_scale(color='independent')

All 4 comments

You can set the scale resolve to be "independent" like here. (From a quick search, I couldn't find Altair syntax page for it, but I'm sure there is one.)

Altair syntax looks like this:

alt.layer(
    bars,
    lines
).resolve_scale(color='independent')

Our docs are definitely missing discussion of resolve for compound charts.

Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LukeMathWalker picture LukeMathWalker  路  3Comments

breadbaron picture breadbaron  路  4Comments

tonylee3399 picture tonylee3399  路  3Comments

HalukaMB picture HalukaMB  路  3Comments

morberg picture morberg  路  3Comments