question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

alt.Repeat - create a grid of charts

See original GitHub issue

Hi Jake,

In the docs you have a nice repeating example that I am trying to learn from. But suppose I have a simple dataFrame like this:

df=pd.DataFrame({'year': [1,2,3], 'school 1': [10,20,30], 'school 2': [15,6,25], 'school 3': [120,202,302], 'school 4': [152,6,225]})

and I would like a 2x2 grid where for each chart “year” is on the x axis and each charts show a line for a given school. So basically, one field is held constant. I can accomplish this by doing the following, but is there a better, more “Altair-ic” way to do this?

visualization 19

y1=alt.Chart().mark_line(point=True).encode(
    x='year',
    y='school 1',
)

y2=alt.Chart().mark_line(point=True).encode(
    x='year',
    y='school 2',
)

y3=alt.Chart().mark_line(point=True).encode(
    x='year',
    y='school 3',
)

y4=alt.Chart().mark_line(point=True).encode(
    x='year',
    y='school 4',
)

alt.vconcat((y1|y2), (y3|y4), data=df)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
Alcampopianocommented, Sep 12, 2018

For anyone looking for an example until this functionality is in VL/Altair. There are various solutions here and my attempt is the following. Note that the DataFrame in this case has three columns and is in long form (see df.melt)

numcols=3 # specify the number of columns you want 
all_categories=df['Category_Column'].unique() # array of strings to use as your filters and titles

rows=alt.vconcat(data=df)
numrows=int(np.ceil(len(all_categories) / numcols))
pointer=0
for _ in range(numrows):

  row=all_categories[pointer:pointer+numcols]
  cols=alt.hconcat()

  for a_chart in row:

     # add your layers here
     # line chart
     line=alt.Chart().mark_line(point=True).encode(
        x='variable',
        y='value'
     ).transform_filter(datum.Category_Column == a_chart).properties(
        title=a_chart, height=200, width=200)

     # text labels
     text=alt.Chart().mark_text().encode(
        x='variable', 
        y='value'
     ).transform_filter(datum.Category_Column == a_chart)

     both = line + text
     cols |= both

  rows &= cols
  pointer += numcols

rows

visualization 22

2reactions
mattijncommented, Mar 5, 2021

I haven’t seen this documented on SO or GitHub issues, but one can also use repeat instead of column or row within repeat:

import altair as alt
from vega_datasets import data

cars = data.cars.url

alt.Chart(cars, width=200, height=150).mark_bar().encode(
    x=alt.X(alt.repeat('repeat'), type='quantitative', bin=alt.Bin(maxbins=20)),
    y='count()'
).repeat(
    repeat=["Horsepower", "Miles_per_Gallon", "Acceleration", "Displacement"], 
    columns=2
)
image
Read more comments on GitHub >

github_iconTop Results From Across the Web

Compound Charts: Layer, HConcat, VConcat, Repeat, Facet
In this example, we explicitly loop over different x and y encodings to create a 2 x 2 grid of charts showing different...
Read more >
Repeated graph in altair - python - Stack Overflow
If you get empty graphs in a repeat chart, it usually means one of two things: your data is not accessible by the...
Read more >
altair line up concatenated charts into a grid - You.com
The columns argument can be similarly specified for concatenated charts in alt.concat() and repeated charts alt.Chart.repeat() . Open side panel.
Read more >
Compound Charts: Layer, HConcat, VConcat, Repeat, Facet
Along with the basic Chart object, Altair provides a number of compound plot types that can be used to create stacked, layered, faceted,...
Read more >
Multi-View Composition - Nextjournal
repeat : take a base chart specification and apply it to multiple data ... The two can be used together to create a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found