带有水平连接图表的两个标题
Two titles with horizontally concatenated charts
我想连接两个图表,然后美化生成的组合图表(添加一些漂亮的背景)。这里的一件重要事情是尝试保留两个图表的标题。
当我尝试这样做时,我要么得到只有一个标题的美化组合图表,要么出现错误:ValueError: Objects with "background" attribute cannot be used within HConcatChart. Consider defining the background attribute in the HConcatChart object instead.
这是我尝试过的一些虚拟代码片段。
尝试 #1,它只产生一个标题:
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')
)
尝试#2 产生值错误:
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
有没有办法实现我想要的?或者 Altair 还不允许这样做?
您可以在单个图表上指定标题,在组合图表上指定背景:
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')
我想连接两个图表,然后美化生成的组合图表(添加一些漂亮的背景)。这里的一件重要事情是尝试保留两个图表的标题。
当我尝试这样做时,我要么得到只有一个标题的美化组合图表,要么出现错误:ValueError: Objects with "background" attribute cannot be used within HConcatChart. Consider defining the background attribute in the HConcatChart object instead.
这是我尝试过的一些虚拟代码片段。
尝试 #1,它只产生一个标题:
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')
)
尝试#2 产生值错误:
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
有没有办法实现我想要的?或者 Altair 还不允许这样做?
您可以在单个图表上指定标题,在组合图表上指定背景:
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')