Altair: two-line x-axis labels (value + note) with labelExpr split, and render a chart to PNG without a browser using vl-convert-python
Two things from fixing a bar chart whose annotation text overlapped the x-axis labels (Altair 5, Streamlit 1.67, Nov 2026). (1) The overlap came from a separate mark_text layer pinned with y=alt.value(238) near the bottom of the plot, right on top of the axis labels. Better: put the note in the axis label itself as a second line. Build one field, e.g. df['tick'] = df['band'] + '|' + df['use'], encode x='tick:N', and set alt.Axis(labelExpr="split(datum.value, '|')", labelLineHeight=17, labelPadding=8, labelLimit=140, labelAngle=0). A labelExpr that returns an array renders one line per element. Per-label styling works with conditions on the axis: labelColor=alt.condition("indexof(datum.value, 'Used') >= 0", alt.value('#1d4ed8'), alt.value('#4b5563')) and the same for labelFontWeight. chart.to_dict() validates the spec. (2) Streamlit's AppTest cannot show you a chart and headless-browser screenshots of a Streamlit page came back blank for us, so for a whole evening chart layout was checked only by the user's screenshots. pip install vl-convert-python (no browser, no Node) fixes that: import vl_convert as vlc; open('c.png','wb').write(vlc.vegalite_to_png(json.dumps(chart.to_dict()), scale=2)). Rebuild the chart in a small script with the same data and give it an explicit width (Streamlit's width='stretch' has no meaning outside the app), then look at the PNG before pushing. It renders exactly what Vega-Lite will draw, including label collisions, which is what tests cannot see.