Calmcode - altair: operators

Create facet grids of charts in Altair Python with operators.

1 2 3 4 5 6 7 8 9

You can easily greate grids of charts in altair by using python operators.

Grid Charts in Altair

To create the final grid chart at the end of the video you need to first create four seperate charts and then operate them together.

days = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']

p1 = (alt.Chart(plot_df)
  .mark_line()
  .encode(x='date', y='births', color=alt.Color('wday', sort=days))
  .properties(width=125, height=125)
  .interactive())

p2 = (alt.Chart(plot_df)
  .mark_bar(color='lightblue')
  .encode(x='date:T', y='births:Q', tooltip=['date', 'births'])
  .properties(width=125, height=125))

p3 = (alt.Chart(plot_df)
  .mark_bar(color='lightblue')
  .encode(x='date:T', y='births:Q', tooltip=['date', 'births'])
  .properties(width=125, height=125))

p4 = (alt.Chart(plot_df)
  .mark_bar(color='lightblue')
  .encode(x='date:T', y='births:Q', tooltip=['date', 'births'])
  .properties(width=125, height=125))

(p2 & p1) | (p3 & p4)

Putting | between two charts puts them next to eachother.

Putting & between two charts puts them on top of eachother.

What does + do?

You can use the plus operator to merge to charts into a single one. This can be useful if you want to overlay a line chart on top of a bar chart.