Reputation: 59
For the example below I want to use green color scheme for exports and red for imports. When I separately create the charts everything is good, they get the color scheme I assign them. However, when I concat the charts both of them get the red scheme.
import pandas as pd
fruits = ['Apples', 'Pears', 'Nectarines', 'Plums', 'Grapes', 'Strawberries']
years = ["2015", "2016", "2017"]
exports = {'fruits' : fruits,
'2015' : [2, 1, 4, 3, 2, 4],
'2016' : [5, 3, 4, 2, 4, 6],
'2017' : [3, 2, 4, 4, 5, 3]}
imports = {'fruits' : fruits,
'2015' : [-1, 0, -1, -3, -2, -1],
'2016' : [-2, -1, -3, -1, -2, -2],
'2017' : [-1, -2, -1, 0, -2, -2]}
df_exp = pd.DataFrame(exports)
df_imp = pd.DataFrame(imports)
import altair as alt
cols_year_imp = df_imp.columns[1:]
cols_year_exp = df_exp.columns[1:]
imp = alt.Chart(df_imp).transform_fold(
list(cols_year_imp)
).mark_bar(
tooltip=True
).encode(
x='value:Q',
y='fruits:N',
color=alt.Color('key:O', scale=alt.Scale(scheme='reds'))
)
exp = alt.Chart(df_exp).transform_fold(
list(cols_year_exp)
).mark_bar(
tooltip=True
).encode(
x=alt.X('value:Q',title="Export"),
y='fruits:N',
color=alt.Color('key:O', scale=alt.Scale(scheme='greens', reverse=True)),
order=alt.Order('key:O', sort='ascending')
)
# imp | exp
imp
exp
alt.hconcat(imp, exp)
output: https://i.sstatic.net/jFvu6.png
Upvotes: 6
Views: 1730
Reputation: 48919
You can use resolve_scale
on the concatenated figure:
alt.hconcat(imp, exp).resolve_scale(color='independent')
Upvotes: 7