Reputation: 781
How do I use relative bar width with altair? According to vega-lite documentation. This code:
{
"data": {"url": "data/seattle-weather.csv"},
"mark": {"type": "bar", "width": {"band": 0.7}},
"encoding": {
"x": {
"timeUnit": "month",
"field": "date"
},
"y": {
"aggregate": "mean",
"field": "precipitation"
}
}
}
produces a bar that occupies 70% of each x band.
How do I set this in altair? If I use something like:
.mark_bar(width={'band':0.7})
I got an error about width not being a number.
How do I set relative bar widths in altair?
Upvotes: 1
Views: 471
Reputation: 942
You could also set the padding between the bars instead, using padding
in the scale
argument of the encoding field. For mark_bar, for example, setting it to 0.3, would set the bar width to 70% of the 'tick space'.
import altair as alt
from vega_datasets import data
source = data.seattle_weather()
alt.Chart(source).mark_bar().encode(
x=alt.X('month(date):O', scale=alt.Scale(padding=0.3)),
y='mean(precipitation):Q'
)
Upvotes: 1
Reputation: 49054
On the current development version of Altair on GitHub, what you suggested should work. As per this post, you should also be able to do the following:
import altair as alt
from vega_datasets import data
source = data.seattle_weather()
alt.Chart(source).mark_bar(
width=alt.RelativeBandSize(0.7)
).encode(
x='month(date):O',
y='mean(precipitation):Q'
)
Upvotes: 0