-
Bar charts have a fairly narrow bar width by default, but if you configure a larger pixel width you get overlapping bars when users resize their browser window. What I need is to set the bar width to say 0.7 of the space between ticks, so they grow and shrink with the chart size and always have a gap between them. In Vega-lite you can do it like this:
However, I can't find any way to do that in Altair, e.g. this throws an exception:
How do you specify proportional bar widths? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Once the next version of Altair is released you can use 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'
) |
Beta Was this translation helpful? Give feedback.
-
In addition to the answer above, you can also change the overall chart width, which will expand the bars. Using a step size indicates how many pixels you want to use for the width of each categorical axis value (ie each bar): import altair as alt
from vega_datasets import data
source = data.seattle_weather()
alt.Chart(source, width=alt.Step(30)).mark_bar().encode(
x='month(date):O',
y='mean(precipitation):Q'
) |
Beta Was this translation helpful? Give feedback.
Once the next version of Altair is released you can use
width=alt.RelativeBandSize(0.7)
. Using the main repo of altair this gives me currently: