Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to align chart titles when `hconcat` is used?

Tags:

python

altair

When two bar charts (one horizontal, one vertical) are shown side-by-side using alt.hconcat, the titles are misaligned even though the heights of the charts are equal. Is there a way to align the titles?

# Makes test dataframe
test = pd.DataFrame({"group":["a","b","c"],"value1":[4,5,6], "value2":[10,12,14]}).reset_index()

# Sets up vertical bar chart
chart1 = alt.Chart(test).mark_bar().encode(
    x = alt.X('group:N'),
    y = alt.Y('value1:Q')
).properties(height = 300, width = 300, title = "Testing Title 1")

# Sets up horizontal bar chart
chart2 = alt.Chart(test).mark_bar().encode(
    x = alt.X('value2:Q'),
    y = alt.Y('group:N')
).properties(height = 300, width = 300, title = "Testing Title 2")

# Shows bar charts side by side
alt.hconcat(chart1, chart2)

The chart titles are misaligned. (can't post the image since I apparently need 10 reputation points to do so)

like image 224
Newbie Avatar asked Oct 15 '22 09:10

Newbie


People also ask

How do you align a chart title?

On the Format tab, in the Current Selection group, click Format Selection. In the Format Chart Title dialog box, click the Alignment category. Under Text Layout, do one of the following: To align text, in the Vertical alignment box, click the option that you want.

How to align the upper left corner of the chart on excel?

For example, if you want to align the chart title to the top left corner of the chart, simply select the chart title then press the top-left arrow button on the add-in window. The margin setting allows you to set the margin to a specific number of points that will offset the chart element from the chart border.


1 Answers

It appears that this behavior is due to automatic dodging of the top tick and its label "6" from the y-axis in the leftmost chart. Altair/Vega lite probably autoadjusts the title offset to the highest element in the chart, and since the tick and its label are above the axis line, the title will be vertically offset compared to the rightmost axis where the topmost tick and its label "a" are well below the end of the axis line.

You can see that this is indeed the issue by hiding the tick label.

Current appearance from your question (red line added for comparison): enter image description here

After removing tick labels: enter image description here

Not quite aligned, hiding the tick as well aligns them perfectly.

After removing tick and its label: enter image description here

To work around this you can manually set the offset of the two titles to be the same. Unfortunately, using .configure_title(offset=0) on the layout to set both titles at the same time does not work since it adds an offset to the values already used to for the automatic dodging.

Instead, I believe you have to set the offset with alt.TitleParams(offset=0) for the leftmost plot or a value in the rightmost plot that brings it up to the same height as the leftmost plot. In this case, "9" seems to be the magical number

With title = alt.TitleParams("Testing Title 2", offset=9) for the rightmost plot: enter image description here

This is probably an issue/feature of Vegalite rather than Altair, so digging around on the issue tracker or posting a new issue asking them to be aligned by default might be helpful.

like image 119
joelostblom Avatar answered Oct 20 '22 10:10

joelostblom