【问题标题】:Two titles with horizontally concatenated charts带有水平连接图表的两个标题
【发布时间】:2021-01-10 11:44:00
【问题描述】:

我想连接两个图表,然后美化生成的组合图表(添加一些漂亮的背景)。这里重要的一件事是尽量保留两个图表的标题。

当我尝试这样做时,我要么得到只有一个标题的美化组合图表,要么得到一个错误:ValueError: Objects with "background" attribute cannot be used within HConcatChart. Consider defining the background attribute in the HConcatChart object instead.

这是我尝试过的一些虚拟代码 sn-ps。

尝试 #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 还不允许这样做?

【问题讨论】:

    标签: altair


    【解决方案1】:

    您可以在单个图表上指定标题,在组合图表上指定背景:

    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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      相关资源
      最近更新 更多