Search code examples
titlealtairx-axis

How to move / align X-axis title on multiple charts with Altair?


I have the 3 charts below and I would like to vertically align the X-axis title of the third chart ("Autres") with the two others

enter image description here

My code is the following

...
base = alt.Chart(data).properties(
    width=250,
    height=250
).add_selection(selector)
base2 = alt.Chart(data.loc[data.time.isin(l1)]).properties(
    width=250,
    height=250
).add_selection(selector)
base3 = alt.Chart(data.loc[data.time.isin(l2)]).properties(
    width=250,
    height=250
).add_selection(selector)

points = base.mark_point(filled=True, size=50,opacity=1).encode(
    x=alt.X('trage',title="Tranche d'âge"),
    y=alt.Y('mean(y)',title='NBV (%CA)',axis=alt.Axis(format='%')),
    color=alt.condition(selector, 'Reseau_pat:O', alt.value('lightgray')),
    #shape=alt.Shape('Segment:O', scale=alt.Scale(range=['circle', 'triangle'])),
    tooltip = ['id','x','mean(y)']
)

timeseries = base2.mark_bar(opacity=1).encode(
    x=alt.X('time', title='Structure de marge (%PM)'),
    y=alt.Y('value',title='', scale=alt.Scale(domain=(-1, 1)),stack=None,axis=alt.Axis(format='.2f')),
    #y2=alt.Y2('mean(duree)'),
    color=alt.Color('Reseau_pat:O',
                    scale=alt.Scale(domain=domain, range=range_),
                    #legend=None
                   ),
    tooltip = ['value']
).transform_filter(
    selector
)

test = base3.mark_bar(opacity=1,size=50).encode(
    y=alt.Y('time', title='',axis=alt.Axis(orient='right')),
    x=alt.X('value', title="Autres",scale=alt.Scale(domain=(0, 40)),stack=None),
    color=alt.Color('Reseau_pat:O',
                    scale=alt.Scale(domain=domain, range=range_),
                    #legend=None
                   ),
    tooltip = ['value']
).transform_filter(
    selector
)

points | timeseries | test

If you have any idea, it would be very apreciated


Solution

  • You can manually shift the title by using the titleY and titleX argument in the Axis. Do this for all plots, using the same value and they align. So something like x=alt.X(...axis=alt.Axis(titleY=42).

    You could also opt for adding a title to each plot using .properties(title='Autres') and remove the x-axis title. Then the titles would align (on top) at least.