79387294

Date: 2025-01-25 18:43:26
Score: 3.5
Natty:
Report link

Ijust found one possible solution myself, but there might be a better one in case I want to use the axis title and the facet title (header) separately. Anyone?

import altair as alt
import pandas as pd
import textwrap 



df = pd.DataFrame({
    'y': [10, 20, 30 , 40] ,
    'x': ['a', 'b', 'a', 'b'] ,
    'facet' : ['case 1', 'case 1', 'case 2', 'case 2'] 
})
# without wrapping the labels
x         = 'x'
y         = 'y'
facet     = 'facet'
xlabel = ["A long long long long long long"+
          "long long long long long long title"]
base = (alt.Chart(df))
bar = (base.mark_bar()
       .encode(
           x       = alt.X(x).axis(labelAngle=0).title(''),
           y       = alt.Y(y),
       ))
txt = (base.mark_text(dy=-5)
       .encode(
           x    = alt.X(x),
           y    = alt.Y(y),
           text = alt.Y(y),
       ))
g = (alt.layer(bar, txt)
     .properties(width=300, height=250)
     .facet(facet=(alt.Facet(facet).header(title=xlabel, titleOrient='bottom',)),
            columns=2)
     .resolve_scale(x='shared')
     )

enter image description here

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Diogo