Reputation: 871
I would like to use one single title that goes across facets for the x-axis in this plot. How to do that in python using altair?
Apparently, Altair does not provide that functionality.
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(xlabel),
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), columns=2)
)
Upvotes: 0
Views: 12
Reputation: 871
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')
)
Upvotes: 0