How to configure Chart Position in HConcatChart in Altair

Viewed 376

I'm trying to horizontally concatenate two charts in altair, but I can't get them to look just like I want them to.

Here is what they look like:

enter image description here

And here is the code I'm using:

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

And basically, I would like to place the chart "Ausbildungsstand" in the middle of the chart area. I mean, I'd like to separate it from the top edge of the canvas.

I can sort of get the result I want by adjusting the height of the charts (if they have the same height, they're aligned), but I'd like to know how to move the chart inside the "canvas".

Thanks in advance for any help.

1 Answers

You can use the alt.hconcat() function and pass center=True. For example:

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

Related