[英]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.所以我正在玩 Cars 数据集,并希望将 R 值添加到散点图中。 So I can use this code to produce a scatter chart using
transform_regression
to add a regression line which is great.因此,我可以使用此代码生成散点图,使用
transform_regression
添加一条很棒的回归线。
from vega_datasets import data
import altair as alt
import pandas as pd
import numpy as np
cars = data.cars()
chart = alt.Chart(cars).mark_circle().encode(
alt.X('Miles_per_Gallon', scale=alt.Scale(domain=(5,50))),
y='Weight_in_lbs'
)
chart + chart.transform_regression('Miles_per_Gallon','Weight_in_lbs').mark_line()
Here is the chart这是图表
Then I am looking get the R-value.然后我正在寻找 R 值。 So can use pandas with this code as I am not sure how to get the R-value with Altair.
所以可以使用 pandas 和这个代码,因为我不确定如何用 Altair 获得 R 值。
corl = cars[['Miles_per_Gallon','Weight_in_lbs']].corr().iloc[0,1]
corl
Now I was wondering how would I go about adding the R-value on the chart as a sort of label?现在我想知道我将如何 go 关于在图表上添加 R 值作为一种 label?
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()
In future versions of Altair, the empty data in the chart will no longer be required.在 Altair 的未来版本中,将不再需要图表中的空数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.