Is there any way to add regression lines to a scatter matrix in Altair? I am trying to add a regression line to a scatter matrix that I create with the following code :
scatter_matrix = (
alt.Chart(movies_df)
.mark_circle(opacity=0.5, size=10)
.encode(
x=alt.X(alt.repeat("column"), type="quantitative", scale=alt.Scale(zero=False)),
y=alt.Y(alt.repeat("row"), type="quantitative", scale=alt.Scale(zero=False)),
)
.properties(height=120, width=120)
.repeat(
column=numeric_cols,
row=numeric_cols,
columns=5,
title="Distribution of Numerical Columns",
)
.configure_axis(labels=False)
)
scatter_matrix
For a single scatter plot I can simply use .transform_regression() and layer the regression line to the base plot like so:
# create base plot
scatter_plot = alt.Chart(movies_df).mark_point().encode(
x="column",y="row")
# add regression line
scatter_plot_line = scatter_plot + scatter_plot.transform_regression("column","row").mark_line()
# call plot
scatter_plot_line
I initially tried applying this same method to the matrix but got AttributeError: 'RepeatChart' object has no attribute 'mark_line' with the code below.
scatter_matrix = (
alt.Chart(movies_df)
.mark_circle(opacity=0.5, size=10)
.encode(
x=alt.X(alt.repeat("column"), type="quantitative", scale=alt.Scale(zero=False)),
y=alt.Y(alt.repeat("row"), type="quantitative", scale=alt.Scale(zero=False)),
)
.properties(height=120, width=120)
.repeat(
column=numeric_cols,
row=numeric_cols,
columns=5,
title="Distribution of Numerical Columns",
)
.configure_axis(labels=False)
)
scatter_matrix_line = (
scatter_matrix
+ scatter_matrix.transform_regression("column", "row").mark_line()
)
scatter_matrix_line
I then tried applying the method to the base scatter matrix following .encode(), but nothing is returned at all when I call the plot. I found a source stating Altair did not yes support regression lines for plot matrices but that was dated two years ago and thought there may be a solution or workaround I could not find.
Any help is appreciated. Thanks!