如何在Altair的HConcatChart中配置图表位置

2024-09-27 18:00:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在altair中水平连接两个图表,但我无法让它们看起来像我想要的那样

下面是它们的样子:

enter image description here

下面是我使用的代码:

pick_ausbildung = alt.selection_single(fields = ["Ausbildungsstand"], on = "mouseover")

ausbildung_chart  = alt.Chart(umfrage,
                             title = "Ausbildungsstand").mark_bar().encode(
    y=alt.Y("Ausbildungsstand", axis = alt.Axis(title = None)),
    x="count()",
    color = alt.condition(pick_ausbildung, 
                          alt.Color("Ausbildungsstand:N", 
                                    legend = None), alt.value("lightgrey")),
    tooltip = ["Ausbildungsstand","count()"]).properties(height=200).add_selection(pick_ausbildung)

g_ausbildung_chart = alt.Chart(umfrage).mark_bar().encode(
    x="Geschlecht",
    y="count()",
    color = "Geschlecht",
    tooltip = ["Geschlecht","count()"]).properties(width=300).transform_filter(pick_ausbildung)

ausbildung_chart|g_ausbildung_chart
基本上,我想把图表“AuxBurdgsStand”放在图表区域的中间。我的意思是,我想把它从画布的顶部边缘分开

我可以通过调整图表的高度得到我想要的结果(如果它们的高度相同,那么它们是对齐的),但是我想知道如何在“画布”中移动图表

提前感谢您的帮助


Tags: titlecountchart图表baraltencodemark
1条回答
网友
1楼 · 发布于 2024-09-27 18:00:29

您可以使用^{}函数并传递center=True。例如:

import altair as alt
import pandas as pd

df = pd.DataFrame({
    'label': ['A', 'B', 'C', 'D', 'E'],
    'value': [3, 5, 4, 6, 2],
})

chart1 = alt.Chart(df).mark_bar().encode(y='label', x='value')
chart2 = alt.Chart(df).mark_bar().encode(x='label', y='value')

alt.hconcat(chart1, chart2, center=True)

enter image description here

相关问题 更多 >

    热门问题