Altair: Building rows of charts in for loop to create data dashboards

Created on 1 Jan 2019  路  6Comments  路  Source: altair-viz/altair

I have an application that begs for building a row (or grid) of charts inside a for loop where the iterated value might a column label in the pandas dataframe. I've read the documentation for hconcat and repeat methods and repeat is well suited to this, however, my application demands that each chart contain an overlay of data from two different dataframes which (I think) precludes the use of the repeat function. Is there anyway to iteratively build a list a charts within the for loop and then display them in a row or grid after the loop is done executing?

Thanks in advance!

Most helpful comment

I think it's possible to do what you're asking about, but it's hard to say without more information.

Regarding the repeat operator and multiple dataframes, you could use the lookup transform if it applies to the way you're combining the multiple dataframes.

Otherwise, you can always use a Python loop to create multiple charts, and then concatenate them when finished. Something like this:

charts = []
for i in range(4):
    charts.append(alt.Chart(df).mark_point())
alt.vconcat(*charts)

All 6 comments

I think it's possible to do what you're asking about, but it's hard to say without more information.

Regarding the repeat operator and multiple dataframes, you could use the lookup transform if it applies to the way you're combining the multiple dataframes.

Otherwise, you can always use a Python loop to create multiple charts, and then concatenate them when finished. Something like this:

charts = []
for i in range(4):
    charts.append(alt.Chart(df).mark_point())
alt.vconcat(*charts)

Hi Jake,
Super helpful information - thanks! I will give that a try.
-Dave

PS - I am giving some presentations to university computer science and engineering departments in the Northeast in the coming months (Dartmouth and Lehigh to start) and will be mentioning altair as one of the exciting new packages to investigate. Please keep up the awesome work!

Thanks, and let me know if you have other questions!

Your tip worked great - thank you!

Now if only plot interactivity was preserved after converting a notebook to html format.......
-Dave

@dbk123 see this for a workaround to maintain interactivity in HTML

https://github.com/altair-viz/altair/issues/329

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Juan-132 picture Juan-132  路  3Comments

zanarmstrong picture zanarmstrong  路  4Comments

pabloinsente picture pabloinsente  路  3Comments

nielsmde picture nielsmde  路  4Comments

DentonGentry picture DentonGentry  路  3Comments