I would like to access a table with the bin counts and intervals from the histogram resulting from the code snippet below. Is there any way to do this?
import altair as alt
from vega_datasets import data
movies = data.movies.url
alt.Chart(movies).mark_bar().encode(
alt.X("IMDB_Rating:Q", bin=True),
y='count()',
)
You can try using the altair_transform
package for this, example from the readme:
import altair as alt
import pandas as pd
import numpy as np
from altair_transform import transform_chart
np.random.seed(12345)
df = pd.DataFrame({
'x': np.random.randn(20000)
})
chart = alt.Chart(df).mark_bar().encode(
alt.X('x', bin=True),
y='count()'
)
new_chart = transform_chart(chart)
new_chart.data
x_binned x_binned2 count
-4.0 -3.0 29
-3.0 -2.0 444
-2.0 -1.0 2703
-1.0 0.0 6815
0.0 1.0 6858
1.0 2.0 2706
2.0 3.0 423
3.0 4.0 22