I'd like to concatenate two charts and then beautify the resulting combined chart (add some nice background). One important thing here is try to preserve titles for both charts.
When I try to do that, I either get the beautified combined chart with only one title or an error: ValueError: Objects with "background" attribute cannot be used within HConcatChart. Consider defining the background attribute in the HConcatChart object instead.
Here are some dummy code snippets of what I've tried.
Try #1 which yields only one title:
import altair as alt
from vega_datasets import data
source = data.cars()
line = alt.Chart(source).mark_line().encode(
x='Year',
y='mean(Miles_per_Gallon)'
)
band = alt.Chart(source).mark_errorband(extent='ci').encode(
x='Year',
y=alt.Y('Miles_per_Gallon', title='Miles/Gallon'),
)
combined = band | line
combined
combined.properties(background = '#f9f9f9',
title = alt.TitleParams(text = 'General title',
subtitle = ['Subtitle'],
font = 'Ubuntu Mono',
fontSize = 22,
color = '#3E454F',
subtitleFont = 'Ubuntu Mono',
subtitleFontSize = 16,
subtitleColor = '#3E454F')
)
Try #2 which yields a value error:
line2 = line.properties(background = '#f9f9f9',
title = alt.TitleParams(text = 'General title 1',
subtitle = ['Subtitle'],
font = 'Ubuntu Mono',
fontSize = 22,
color = '#3E454F',
subtitleFont = 'Ubuntu Mono',
subtitleFontSize = 16,
subtitleColor = '#3E454F')
)
band2 = band.properties(background = '#f9f9f9',
title = alt.TitleParams(text = 'General title 2',
subtitle = ['Subtitle'],
font = 'Ubuntu Mono',
fontSize = 22,
color = '#3E454F',
subtitleFont = 'Ubuntu Mono',
subtitleFontSize = 16,
subtitleColor = '#3E454F')
)
line2 | band2
Is there a way to achieve what I want? Or Altair doesn't allow this yet?
question from:
https://stackoverflow.com/questions/65647305/two-titles-with-horizontally-concatenated-charts 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…