Apologies if this has been asked before, but I'm looking for a way to create bar-charts that are "dodged" (language from ggplot2) using the Altair library in python.
I know Altair has this example:
import altair as alt
from vega_datasets import data
source = data.barley()
alt.Chart(source).mark_bar().encode(
x='year:O',
y='sum(yield):Q',
color='year:N',
column='site:N'
)
That produces this plot:

However, this has a lot of redundant labels and information. Ideally I want a plot where the paired bars encode the year in colour (blue is 1931 and orange is 1932) and then the cities running along the x-axis (ordinal variable).
Hard to explain, but here's an example of how to get a plot like this from seaborn (using different data; source isthis SO question):

Yes, you've found the recommended way to create grouped bar charts in Altair. If you want to adjust the final look of the chart, you can do things like removing & rearranging labels and titles; here's how you might modify your example to make it closer to the seaborn chart:
import altair as alt
from vega_datasets import data
source = data.barley()
alt.Chart(source).mark_bar().encode(
x=alt.X('year:O', axis=alt.Axis(title=None, labels=False, ticks=False)),
y=alt.Y('sum(yield):Q', axis=alt.Axis(grid=False)),
color='year:N',
column=alt.Column('site:N', header=alt.Header(title=None, labelOrient='bottom'))
).configure_view(
stroke='transparent'
)

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With