Vega-Lite/Altair extend regression line to the edges of the graph

458 views Asked by At

I'm trying to find a way to extend regression lines in vega-lite/altair charts to the edge of the chart. As of now, when applying a regression transform to a dataset results in datapoints that only stretch to the bounding-box of the original dataset. Is it possible somehow to extend this range to the x/y extents of the chart? In the picture below, the black line is what vega-lite calculates per default. Extending the line to the edges as shown in yellow is what I'm trying to achieve.

enter image description here

EDIT

When specifying the extent property on the transform_regression call it seems like it is adjusting the y variable instead of the x variable. Maybe I'm grossly misunderstanding something but maybe it has something to do with the fact that my x variable are dates which might behave differently?

When I specify the extent like so

CDR_base.transform_regression(
    'per_capita', 
    'year', 
    groupby=['region'],
    extent=[2000, 2100]
  ).mark_line()

I would expect the extent of the regression lines to extend from 2000 to 2100. For some reason the extent gets applied to the y axis it seems. enter image description here

1

There are 1 answers

4
jakevdp On

You can use the extent argument of the regression transform to control the extent of the line. For example, here is a dataset with a default line:

import altair as alt
import pandas as pd
import numpy as np
np.random.seed(2)
df = pd.DataFrame({
    'x': np.random.randint(0, 100, 10),
    'y': np.random.randint(0, 100, 10)
})
points = alt.Chart(df).mark_point().encode(
    x='x:Q',
    y='y:Q'
)
points + points.transform_regression('x', 'y').mark_line()

enter image description here

And here it is with extent set:

points + points.transform_regression('x', 'y', extent=[0, 90]).mark_line()

enter image description here