使用 Altair 创建包含 3 个图表的合并图表,但第一个标题未正确对齐
Using Altair to create a merged chart with 3 charts but the first title is not aligning properly
这是我创建的图表:
但这就是我想要的样子:
“Top Third”的标题没有居中,但当我尝试调整标题对齐方式时,出现无法使用 hconcat 配置的错误。这是可视化的部分代码:
chart_top = (text_top + bars_top).properties(
width=80,
height=180,
title="Top third",
)
chart_middle = (text_middle + bars_middle).properties(
width=80,
height=180,
title="Middle third",
)
chart_bottom = (text_bottom + bars_bottom).properties(
width=80,
height=180,
title="Bottom third",
)
merge = alt.hconcat(chart_top, chart_middle, chart_bottom).configure_scale(
bandPaddingInner=0.2
).configure_view(
strokeWidth=0
).properties(
title={"text":["How People Rate the 'Star Wars' Movies"],
"subtitle":["How often each film was rated in the top, middle and bottom third",
'(by 471 respondents who have seen all six films)']}
).configure_title(
fontSize=24, align='left', fontWeight='bold', anchor="start", subtitleFontWeight='lighter'
).configure_axis(
labelFontSize=14)
alt.themes.enable('fivethirtyeight')
merge
您可以在最左侧的图表上使用 alt.TitleParams
,并使用 dx
手动设置偏移量或 anchor=end
右对齐:
chart_top = (text_top + bars_top).properties(
title=alt.TitleParams("Top third", dx=100))
您也可以创建一个只包含标签的空图表。
这是我创建的图表:
但这就是我想要的样子:
“Top Third”的标题没有居中,但当我尝试调整标题对齐方式时,出现无法使用 hconcat 配置的错误。这是可视化的部分代码:
chart_top = (text_top + bars_top).properties(
width=80,
height=180,
title="Top third",
)
chart_middle = (text_middle + bars_middle).properties(
width=80,
height=180,
title="Middle third",
)
chart_bottom = (text_bottom + bars_bottom).properties(
width=80,
height=180,
title="Bottom third",
)
merge = alt.hconcat(chart_top, chart_middle, chart_bottom).configure_scale(
bandPaddingInner=0.2
).configure_view(
strokeWidth=0
).properties(
title={"text":["How People Rate the 'Star Wars' Movies"],
"subtitle":["How often each film was rated in the top, middle and bottom third",
'(by 471 respondents who have seen all six films)']}
).configure_title(
fontSize=24, align='left', fontWeight='bold', anchor="start", subtitleFontWeight='lighter'
).configure_axis(
labelFontSize=14)
alt.themes.enable('fivethirtyeight')
merge
您可以在最左侧的图表上使用 alt.TitleParams
,并使用 dx
手动设置偏移量或 anchor=end
右对齐:
chart_top = (text_top + bars_top).properties(
title=alt.TitleParams("Top third", dx=100))
您也可以创建一个只包含标签的空图表。