Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

altair - Two titles with horizontally concatenated charts

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 #1 chart

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can specify the titles on the individual charts, and the background on the combined chart:

line2 = line.properties(
                    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(
                    title = alt.TitleParams(text = 'General title 2', 
                                            subtitle = ['Subtitle'],
                                            font = 'Ubuntu Mono', 
                                            fontSize = 22, 
                                            color = '#3E454F', 
                                            subtitleFont = 'Ubuntu Mono',
                                            subtitleFontSize = 16, 
                                            subtitleColor = '#3E454F')
                  )

combined = band2 | line2
combined.properties(background = '#f9f9f9')

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...