1

I am trying to create a grouped bar chart in altair like in the answer to this question here.

The particular interesting part is the "beautification:

chart = Chart(df).mark_bar().encode(
   column=Column('Genre', 
                 axis=Axis(axisWidth=1.0, offset=-8.0, orient='bottom'),
                 scale=Scale(padding=4.0)),
   x=X('Gender', axis=False),
   y=Y('Rating', axis=Axis(grid=False)),
   color=Color('Gender', scale=Scale(range=['#EA98D2', '#659CCA']))
).configure_facet_cell(
    strokeWidth=0.0,
)

chart.display()

The issue is, however, that none of the stuff in the columns (alt.Column) works in the current version of Altair (I am using 4.2).

In particular, I am getting:

SchemaValidationError: Invalid specification altair.vegalite.v4.schema.channels.Column, validating 'additionalProperties' Additional properties are not allowed ('axis' was unexpected)

Can something similar still be done?

joelostblom
  • 43,590
  • 17
  • 150
  • 159
clog14
  • 1,549
  • 1
  • 16
  • 32

1 Answers1

1

In Altair 4.2.0 you achieve a similar results like this (not sure if you can connect the facets with the x-axis line):

import altair as alt
import pandas as pd

# create dataframe
df = pd.DataFrame([['Action', 5, 'F'], 
                   ['Crime', 10, 'F'], 
                   ['Action', 3, 'M'], 
                   ['Crime', 9, 'M']], 
                  columns=['Genre', 'Rating', 'Gender'])

chart = alt.Chart(df).mark_bar().encode(
   column=alt.Column(
       'Genre', 
       header=alt.Header(orient='bottom')
    ),
   x=alt.X('Gender', axis=alt.Axis(ticks=False, labels=False, title='')),
   y=alt.Y('Rating', axis=alt.Axis(grid=False)),
   color='Gender'
).configure_view(
    stroke=None,
)

chart

enter image description here

In the current development version of Altair (will probably be released as 5.0), you can use the new offset channels to achieve the same result without faceting:

chart = alt.Chart(df).mark_bar().encode(
   x=alt.X('Genre', axis=alt.Axis(labelAngle=0)),
   xOffset='Gender',
   y=alt.Y('Rating', axis=alt.Axis(grid=False)),
   color='Gender'
).configure_view(
    stroke=None,
)

chart

enter image description here

joelostblom
  • 43,590
  • 17
  • 150
  • 159
  • Hey thanks! The only thing that is not working is `header=alt.Header(orient='bottom')`. This specifically leaves the header and the labels just above (i.e. in the original position). When I do `orientTitle` the title (of the column header) gets correctly moved down. But when I do `orientLabel` I seem to run into this here: https://github.com/altair-viz/altair/issues/2557. Do I have an incorrect version of Altair? – clog14 May 02 '22 at 18:56
  • Also one other question - is it possible to replicate a chart like this downwards or to the right (or both?) - if you had another dimension say country of birth or something similar in the above example? Thanks again!! – clog14 May 02 '22 at 19:07
  • @clog14 I think the header orientation was fixed in altair 4.2.0, so it is possible that you need to restart your JupyterLab if you recently installed the new version of Altair. Make sure to close any open notebooks, or the version of VegaLite might not be updated correctly. Could you open a new question with an example for your second comment? – joelostblom May 02 '22 at 22:11
  • This does not work in 5.0.1 despite having xOffset support. It just shows 2 stacked bars. – Cassova Jun 14 '23 at 09:23
  • @Cassova It works for me, if you recently upgraded to altair 5, make sure you have closed any notebook using altair 4, and then restart your IDE. Having figures with altair 4 plots showing can prevent the upgrade. If that doesn't work, please submit a bug report on github with more details – joelostblom Jun 15 '23 at 00:02