I'm trying to make a plot:
import altair as alt
from vega_datasets import data
movies = data.movies.url
base = alt.Chart(movies).mark_bar().encode(
alt.Y('count()')).properties(
width=200,
height=150
)
chart = alt.vconcat()
for x_encoding in ['IMDB_Rating:Q', 'IMDB_Votes:Q']:
row = alt.hconcat()
for maxbins_encoding in [10, 50]:
row |= base.encode(alt.X(x_encoding,
type='quantitative',
bin=Bin(maxbins=maxbins_encoding)))
chart &= row
chart
This works. Then I'm trying to use alt.repeat():
alt.Chart(vega_datasets.data.movies.url).mark_bar().encode(
alt.X(alt.repeat("row"), type='quantitative',
bin=Bin(maxbins=alt.repeat('column'))),
alt.Y('count()')
).properties(
width=200,
height=150
).repeat(
row=['IMDB_Rating', 'IMDB_Votes'],
column=[10, 50]
)
It gives me this error message:
SchemaValidationError: Invalid specification
altair.vegalite.v3.schema.core.BinParams->maxbins, validating 'type'
{'repeat': 'column'} is not of type 'number'
So I must have missed something. Does it have something to do with using repeat() in the bin=Bin() argument other than directly using it in the encode()?
Unfortunately, repeat entries cannot be used for bin parameters. The only parameters that use repeat in Vega-Lite are column names passed to encodings, so your initial approach of looping is probably best.
If you want to take advantage of repeats for the x encodings, you could do something like this:
def make_column(maxbins):
return alt.Chart(movies).mark_bar().encode(
alt.X(alt.repeat("row"), type='quantitative',
bin=alt.Bin(maxbins=maxbins)),
alt.Y('count()')
).properties(
width=200,
height=150
).repeat(
row=['IMDB_Rating', 'IMDB_Votes'],
)
make_column(10) | make_column(50)

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