Rotated column headers in Altair have uneven offset

Viewed 377

Just started learning Altair. I have a simple grouped bar plot where I experimented with rotating the headers. With no rotation I do not see any issue but when I rotate it there is an offset that I cannot find any solution to reduce -
Code -

import altair as alt
import pandas as pd
dummy = pd.DataFrame({'place':['Asia', 'Antarctica','Asia', 'Antarctica'],'category':['a','a', 'b', 'b'],'value':[1,2,3,4]})
alt.Chart(dummy).mark_bar().encode(
    x='category',
    y='value',
    column=alt.Column('place:N', header=alt.Header(labelAngle=-90)),
    color='category',
)

The offset in the graph (Notice the difference in gap from plot b/w Antarctica and Asia)-
enter image description here
Without the rotation -
enter image description here
Expected output -
Rotated but without the large gaps.

1 Answers

This is a known bug in the vega renderer; see https://github.com/altair-viz/altair/issues/1878 for a variant and for links to the core issue.

The workaround in this case is to set labelAlign='right':

import altair as alt
import pandas as pd
dummy = pd.DataFrame({'place':['Asia', 'Antarctica','Asia', 'Antarctica'],'category':['a','a', 'b', 'b'],'value':[1,2,3,4]})
alt.Chart(dummy).mark_bar().encode(
    x='category',
    y='value',
    column=alt.Column('place:N', header=alt.Header(labelAngle=-90, labelAlign='right')),
    color='category',
)

enter image description here

Related