Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View data after transformation

Tags:

python

altair

Is it possible to see the data after Altair applies transformations and aggregations?

For example, can you access the underlying data after the following transformations?

import altair as alt
from vega_datasets import data

source = data.seattle_weather.url

step = 20
overlap = 1

alt.Chart(source, height=step).transform_timeunit(
    Month='month(date)'
).transform_joinaggregate(
    mean_temp='mean(temp_max)', groupby=['Month']
).transform_bin(
    ['bin_max', 'bin_min'], 'temp_max'
).transform_aggregate(
    value='count()', groupby=['Month', 'mean_temp', 'bin_min', 'bin_max']
).transform_impute(
    impute='value', groupby=['Month', 'mean_temp'], key='bin_min', value=0
).mark_area(...
)

Above code from the Ridgeplot example

like image 863
jtzupan Avatar asked Apr 10 '26 09:04

jtzupan


1 Answers

Transforms are evaluated in Javascript, and there is not any built-in way to access data in the javascript frontend from the Python backend. However, there is an experimental package called altair_transform that is able to evaluate most Vega expressions in Python.

For your chart you can use it like this:

import altair as alt
from vega_datasets import data

source = data.seattle_weather()

step = 20
overlap = 1

chart = alt.Chart(source, height=step).transform_timeunit(
    Month='month(date)'
).transform_joinaggregate(
    mean_temp='mean(temp_max)', groupby=['Month']
).transform_bin(
    ['bin_max', 'bin_min'], 'temp_max'
).transform_aggregate(
    value='count()', groupby=['Month', 'mean_temp', 'bin_min', 'bin_max']
).transform_impute(
    impute='value', groupby=['Month', 'mean_temp'], key='bin_min', value=0
).mark_area().encode(
    x='Month:T',
    y='value:Q'
)

import altair_transform
data = altair_transform.extract_data(chart)
print(data)
     bin_min      Month  mean_temp  bin_max  value
0        0.0 1900-01-01   8.229032     -5.0    2.0
1        5.0 1900-01-01   8.229032      0.0   19.0
2       10.0 1900-01-01   8.229032      5.0   72.0
3       15.0 1900-01-01   8.229032     10.0   29.0
4       20.0 1900-01-01   8.229032     15.0    2.0
..       ...        ...        ...      ...    ...
103     20.0 1900-12-01   8.194355     15.0    4.0
104     25.0 1900-12-01   8.194355      NaN    0.0
105     30.0 1900-12-01   8.194355      NaN    0.0
106     35.0 1900-12-01   8.194355      NaN    0.0
107     40.0 1900-12-01   8.194355      NaN    0.0

[108 rows x 5 columns]
like image 57
jakevdp Avatar answered Apr 12 '26 23:04

jakevdp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!