I have a large plot with a long legend. Is it possible to add vertical space after certain items?
For instance, if I use the plot shown here as an example, is it possible to add additional vertical space between the items fog-rain and snow-sun (this is of course just an arbitrary example)?
Here is a copy of the code from the altair documentation:
import altair as alt
from vega_datasets import data
source = data.seattle_weather()
alt.Chart(source).mark_bar(
cornerRadiusTopLeft=3,
cornerRadiusTopRight=3
).encode(
x='month(date):O',
y='count():Q',
color='weather:N'
)
Thank you very much.
You could do something like this:
source = data.seattle_weather.url
alt.Chart(source).mark_bar().encode(
x='month(date):O',
y='count():Q',
color=alt.Color(
'weather:N',
legend=alt.Legend(
labelExpr='indexof(["snow", "fog"], datum.label) != -1 ? [datum.label, ""] : datum.label'
)
)
)
This works by checking if the label datum.label
exists in the list ['snow', 'fog'] by asking for the index of the item in that list (which is -1
if the item doesn't exist). If that's the case, we pass a list of two strings, which is how VegaLite/Altair defines multiline strings/titles.
There is also rowPadding
which changes the padding between all rows, but I don't think it has access to the column values to do comparisons the way it it done in labelExpr
.