Skip to content Skip to sidebar Skip to footer

Adding R-value (correlation) To Scatter Chart In Altair

So I am playing around with the Cars dataset and am looking to add the R-value to a scatter chart. So I can use this code to produce a scatter chart using transform_regression to a

Solution 1:

You can do this by adding a text layer:

text = alt.Chart({'values':[{}]}).mark_text(
    align="left", baseline="top"
).encode(
    x=alt.value(5),  # pixels from left
    y=alt.value(5),  # pixels from top
    text=alt.value(f"r: {corl:.3f}"),
)

chart + text + chart.transform_regression('Miles_per_Gallon','Weight_in_lbs').mark_line()

enter image description here

In future versions of Altair, the empty data in the chart will no longer be required.

Post a Comment for "Adding R-value (correlation) To Scatter Chart In Altair"