Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

python - How to configure Chart Position in HConcatChart in Altair

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.

question from:https://stackoverflow.com/questions/66067285/how-to-configure-chart-position-in-hconcatchart-in-altair

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...