如何解决两个带有水平连接图表的标题
我想连接两个图表,然后美化生成的组合图表(添加一些漂亮的背景)。这里的一件重要事情是尽量保留两个图表的标题。
当我尝试这样做时,我要么得到只有一个标题的美化组合图表,要么出现错误:ValueError: Objects with "background" attribute cannot be used within HConcatChart. Consider defining the background attribute in the HConcatChart object instead.
以下是我尝试过的一些虚拟代码片段。
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',subtitleColor = '#3E454F')
)
band2 = band.properties(background = '#f9f9f9',title = alt.TitleParams(text = 'General title 2',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',subtitleColor = '#3E454F')
)
combined = band2 | line2
combined.properties(background = '#f9f9f9')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。