Search code examples
altairvega-lite

altair heatmap: how to apply axis labels wrap, without losing meaning in values and cell values?


I have some long label in axis (see the example below). Here in the vega editor.

enter image description here

If I apply wrap

df['l_x'] = df['l_x'].apply(wrap, args=[10])
df['l_y'] = df['l_y'].apply(wrap, args=[10])

it works for the labels, but the colors in my graph lose meaning (look at the new 1️⃣ white cells and 2️⃣ generally to the new colors of cells) and the cell labels disappear, probably because the output lists break everything. Here in the vega editor. How to avoid this?

enter image description here


Solution

  • I have first applied python wrap and then a search and replace in my fields

    wrap=10
    df['l_x'] = df['l_x'].str.wrap(wrap)
    df['l_y'] = df['l_y'].str.wrap(wrap)
    df['l_x'] = df['l_x'].str.replace('\n', '@')
    df['l_y'] = df['l_y'].str.replace('\n', '@')
    

    Then I have set for x and y

    axis=alt.Axis(labelExpr="split(datum.Label,'@')")
    

    And now it works. Thank you Casey Haber.

    enter image description here