change color of bars in a concated chart(two bar chart) in altair

Viewed 994

i have two bar chart that vertically stacked. i use blow code for change color of them.

    alt.Color('variable', domain=['a','b','c'],range=['#for_a','#for_b','#for_c']

problem is, when i set color for one of them, other chart disappear or, when set color for both (when i set domain and range for both), color of one affect other bar. how solve that?

chart_a = alt.Chart(dfA).mark_bar().encode(
    alt.X('monthdate(date):O'),
    alt.Y('value'),
    alt.Color('variable',
             scale=alt.Scale(domain=['a','b'],
                            range=['green','yellow'])
             )


chart_b =alt.Chart(dfB).mark_bar().encode(
    alt.X('monthdate(date):O'),
    alt.Y('value'),
    alt.Color('variable',
              scale=alt.Scale(domain=['c','d','e','f'],
                             range=['#ffbb9e','#992e00','#9ecdff','#005799']),
             )
)
chart_a & chart_b
1 Answers

Altair shares scales between concatenated charts by default. To change this, you can use the tools covered in Scale and Guide Resolution.

In your case, it sounds like you want independent color scales:

(chart_a & chart_b).resolve_scale(color='independent')
Related